array matching
Jean-Michel Pichavant
jeanmichel at sequans.com
Fri Apr 30 05:23:28 EDT 2010
Bill Jordan wrote:
> Hey guys,
>
> I am sorry if this is not the right list to post some questions. I
> have a simple question please and would appreciate some answers as I
> am new to Python.
>
> I have 2 D array: test = [[A,1],[B,2],[A,3][B,4]]
> I want to arrang this array in different arrays so each one will have
> what is attached to. For example I want the output:
>
> A =[1,3] and B=[2,4]
>
> Thanks,
> Bill
>
Did you try anything before asking ? this is a pretty simple problem.
test = [['A',1],['B',2],['A',3], ['B',4]]
arranged ={} # dictionary containing the arranged arrays
for name, value in test: # you better be sure all arrays contain only 2
elements
if name in arranged:
arranged[name].append(value)
else:
arranged[name] = [value]
print arranged
out: {'A': [1, 3], 'B': [2, 4]}
JM
More information about the Python-list
mailing list