Python - How to add add values between row and column in ndarray

Asked

Viewed 336 times

1

Hello, I am having the following difficulty, this because I am working with numpy now, I am inexperienced...

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

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

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

then how could I do it?

I’ve seen the question Shape, But I don’t understand it very well. I tried the np.append(array, value, Axis), changing the value of Axis 0 to 1, but it didn’t work.

Input example:

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

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

Exit:

[  'V'     , 'NPROP',           'N',         'PREP'
 ['São'], ['Paulo', 'Edu'], ['18', '1988'], ['de', 'de']
]
  • 1

    You could include an example of input and its expected output ?

  • @Lacobus I changed the question... tell me if you have given of enteder!

  • And what the exit would look like ?

  • @Lacobus Pronto!

1 answer

2


To solve your problem, a dicionário of listas would be more appropriate than a NumPy array, look at you:

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)

Exit:

{'N': ['18', 'julho', '1988'], 'NPROP': ['Paulo', 'Edu'], 'PREP': ['de', 'de'], 'V': ['Sao']}
  • as implemented the method has_key() ?

  • I made the parole stand if has_key(b) for if b not in dic, who has the same behavior and becomes more readable.

  • I get it... I’ll test it here. Thank you very much for your help....

Browser other questions tagged

You are not signed in. Login or sign up in order to post.