Python - How to merge row and column values into ndarray

1

Hello, I'm having the following difficulty, because I'm working with numpy now, I'm inexperienced ...

Well, my problem is this: I have a tuple (w, t), where t repeats, so I want to add t as lines, but specifically in the first row of the array, then I want to check which tuples have the t and add the column it belongs to, it would look like this:

[    t1   ,       t2     ...
  (w1, t1) ,   (w2, t2), ...
   .
   .
   .
]

so that t will always be added in the first row on the x-axis, and the w's in the columns on the y-axis.

So, how could you do that?

I've seen the issue shape , but I can not quite understand it. I tried np.append (array, value, axis) , changing the value of axis from 0 to 1, but did not work.

Example input:

[('São', 'V'), ('Paulo', 'NPROP'), ('18', 'N'), ('de', 'PREP'), ('julho', 'N'), ('de', 'PREP'), ('1988', 'N'), ('Edu', 'NPROP')]... 

A list with tuples ... in the case I wanted to remove the duplicity of the value from the right tuple by putting it as a column, and the values from the left would be inside a list in its respective column.

Output:

[  'V'     , 'NPROP',           'N',         'PREP'
 ['São'], ['Paulo', 'Edu'], ['18', '1988'], ['de', 'de']
]
    
asked by anonymous 04.11.2017 / 04:00

1 answer

2

To solve your problem, a dicionário of listas would be more appropriate than a%

lista = [('Sao', 'V'), ('Paulo', 'NPROP'), ('18', 'N'), ('de', 'PREP'), ('julho', 'N'), ('de', 'PREP'), ('1988', 'N'), ('Edu', 'NPROP')]

dic = {}

for a, b in lista:
    if b not in dic:
        dic[b] = [a]
    else:
        dic[b].append(a)

print(dic)

Output:

{'N': ['18', 'julho', '1988'], 'NPROP': ['Paulo', 'Edu'], 'PREP': ['de', 'de'], 'V': ['Sao']}
    
04.11.2017 / 20:53