Saturday, 15 September 2012

python - Create list for each unique value -


I am currently looking at the table with the following structure.

  UID | Action 1 | A1 1 A1 1 A1 1 A4 2. A1 2. A8 2 A 9 3 A3 3. A7  

I am trying to create a multi-dimensional array with the following structure. To track actions of

  [[A1, A1, A1, A4], [A1,  uid ] and appending actions to a list up to  For my consideration I , A9], [A3, A7]]  

key change once the uid key changes , Then all tasks will be added to another array and converted to tracked uid new uid .

I have come up with some overflow and wrong solution using the itertools.groupby () , but I am not satisfied with it and am looking for some simplicity. However, I have guessed this problem and I am coming up with more complex solutions.

Any suggestion would be appreciated.

code:

  for data = [], j itertools.groupby (table, key = lambda x: x ['uid']): event_array = [ ] In the list for Kashmir: event_array.append (k ['action']) Data.append ([i, event_array])  

,

@black Are you sure the data is ordered?

... @thefourtheye, yes very sure that I had to write it in sql before reading it in Python

Since the data has already been ordered, For example, like this

  & gt; & Gt; & Gt; Data = [['Action': 'A1', 'UID': 1}, ... {'Action': 'A1', 'UID': 1}, ... {'Action': 'A 1 '' AID ': 1}, ... {' Action ':' A4 ',' UID ': 1}, ... {' Action ':' A1 ',' UID ': 2},. .. {Action: 'A8', 'UID': 2}, ... {'Action': 'A9', 'UID': 2}, ... {'Action': 'A3' , 'UID': 3}, ... {'verb': 'A7', 'UID': 3}]  

You simply group with the nested list < / Code> You can use this kind of understanding,

  gt; & Gt; ([A '1', 'A1', 'A1', '1', 'A1', 'A4'], ['A1', 'A8', 'A9'], ['A3', 'A7']]  

No comments:

Post a Comment