Reputation: 321
for x in mylist:
print x
Output is similar to:
['2009/09/13_00:00', 'R']
['2009/09/13_00:00', 'R']
['2009/09/13_00:00', 'R']
['2009/09/13_00:00', 'R']
['2009/09/13_00:00', 'R']
['2009/09/13_00:00', 'R']
['2009/09/13_00:00', 'J']
['2009/09/13_00:00', 'J']
['2009/09/16_00:00', 'R']
['2009/09/18_00:00', 'R']
['2009/09/19_00:00', 'R']
['2009/09/23_00:00', 'R']
['2009/09/24_00:00', 'R']
['2009/09/24_00:00', 'W']
['2009/09/24_00:00', 'S']
['2009/09/24_00:00', 'S']
['2009/09/24_00:00', 'T']
How can I make it so it will the duplicates in the same date will be joined together?
For example, the output I want it to be:
['2009/09/13_00:00', 'R, R, R, R, R, R']
['2009/09/13_00:00', 'J, J']
['2009/09/16_00:00', 'R']
['2009/09/18_00:00', 'R']
['2009/09/19_00:00', 'R']
['2009/09/23_00:00', 'R']
['2009/09/24_00:00', 'R']
['2009/09/24_00:00', 'W']
['2009/09/24_00:00', 'S, S']
['2009/09/24_00:00', 'T']
I'm trying to graph this.
Thanks for any help.
Upvotes: 1
Views: 42
Reputation: 25478
Or more explicitly, use a dictionary: collect the dates and letters as keys and keep track of the number of letter occurences:
mydict = {}
for date, letter in mylist:
try:
mydict[(date, letter)] += 1
except KeyError:
mydict[(date, letter)] = 1
for date, letter in sorted(mydict.keys()):
n = mydict[date, letter]
print [date, ','.join([letter]*n)]
Upvotes: 0
Reputation: 5275
mylist= [['2009/09/13_00:00', 'R'],
['2009/09/13_00:00', 'R'],
['2009/09/13_00:00', 'R'],
['2009/09/13_00:00', 'R'],
['2009/09/13_00:00', 'R'],
['2009/09/13_00:00', 'R'],
['2009/09/13_00:00', 'J'],
['2009/09/13_00:00', 'J'],
['2009/09/16_00:00', 'R'],
['2009/09/18_00:00', 'R'],
['2009/09/19_00:00', 'R'],
['2009/09/23_00:00', 'R'],
['2009/09/24_00:00', 'R'],
['2009/09/24_00:00', 'W'],
['2009/09/24_00:00', 'S'],
['2009/09/24_00:00', 'S'],
['2009/09/24_00:00', 'T'],
]
for i in list(set([ tuple(i) for i in mylist])):
print [list(i) +[list(i)[-1]] * (mylist.count(list(i))-1) ]
Output
:
[['2009/09/13_00:00', 'R', 'R', 'R', 'R', 'R', 'R']]
[['2009/09/16_00:00', 'R']]
[['2009/09/24_00:00', 'T']]
[['2009/09/24_00:00', 'S', 'S']]
[['2009/09/13_00:00', 'J', 'J']]
[['2009/09/24_00:00', 'R']]
[['2009/09/18_00:00', 'R']]
[['2009/09/19_00:00', 'R']]
[['2009/09/23_00:00', 'R']]
[['2009/09/24_00:00', 'W']]
Upvotes: 1