Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Sorting data
#1
Hello,

Suppose a regular expression that extracts, say, the characters below and outputs them in the following order:
A
B
C
D

My goal is to organize this output in a table so that A and B are in the same row but in different columns and so on:
A B
C D

Would anyone be able to give me an idea how this can be done?
Thanks!
Reply
#2
Instead of printing the strings found, you could append them to a list, then use list manipulation methods to reorganize the data.
pprod likes this post
Reply
#3
H

I've found this way

import numpy as np

MyList = ['A', 'D', 'B', 'C']
MyArray = np.array(MyList)
MyArraySorted = MyArray[MyArray[:].argsort()]
MyArraySorted2Dmatrix = MyArraySorted.reshape((2,2))
print(MyArraySorted2Dmatrix)
Result
Quote:[['A' 'B']
['C' 'D']]
pprod likes this post
Reply
#4
(Jan-21-2021, 01:56 PM)paul18fr Wrote: H

I've found this way

import numpy as np

MyList = ['A', 'D', 'B', 'C']
MyArray = np.array(MyList)
MyArraySorted = MyArray[MyArray[:].argsort()]
MyArraySorted2Dmatrix = MyArraySorted.reshape((2,2))
print(MyArraySorted2Dmatrix)
Result
Quote:[['A' 'B']
['C' 'D']]


Thank you for your reply. I replaced 'A', 'B', 'C' and 'D' with the variables in which I stored the results of regular expressions and it does produce the result you showed but I want to display the content of the variables arranged in columns to later export as csv, not the variables themselves. Sorry, I am a beginner and I didn't explain myself properly.

Output:
[['cpt_matches' 'e_matches'] ['n_matches' 'wd_matches']]
Thanks.
Reply
#5
Not sure to understand what's your need ...
  • what's the type of your variables (integer, float, string, other)?
  • is each variable a vetor or a matrix?

I'm wondering if you hadn't better use a dictionnary (as the following code) when A,B,C,D are matrixes?

import numpy as np


## first trial on strings 
MyList = ['A', 'D', 'B', 'C']
MyArray = np.array(MyList)
MyArraySorted = MyArray[MyArray[:].argsort()]
MyArraySorted2Dmatrix = MyArraySorted.reshape((2,2))
print("First trial = {}".format(MyArraySorted2Dmatrix))

## now using a dictionnary
n = 10_000
m = 1
# Values are Matrixes
MatrixA = np.random.random((n,m))
MatrixB = np.random.random((n,m))
MatrixC = np.random.random((n,m))
MatrixD = np.random.random((n,m))
MyDictionnary = {'A': MatrixA, 'D': MatrixD, 'B': MatrixB, 'C': MatrixC} 

# Keys are 'A', 'B' and so on
# keys are extracted and converted into a list
ExtractKeys = list(MyDictionnary.keys())

# then you can proceed as previously
MyArray2 = np.array(ExtractKeys)
MyNewSortedArray = MyArray2[np.array(MyArray2)[:].argsort()].reshape((2,2))
print("New trial = {}".format(MyNewSortedArray))

# To extract values
ExtractValueA = MyDictionnary["A"] 
Hope it'll give you ideas
pprod likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Sorting table data Blacktime2 1 1,292 Feb-26-2022, 07:05 PM
Last Post: ibreeden
  Problems Sorting Data in an External File (.txt) Superlegend21 1 4,286 Dec-27-2020, 10:06 PM
Last Post: Superlegend21
  Sorting data into columns pprod 0 1,945 Nov-18-2020, 06:20 PM
Last Post: pprod
  Sorting a copied list is also sorting the original list ? SN_YAZER 3 2,996 Apr-11-2019, 05:10 PM
Last Post: SN_YAZER

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020