简体   繁体   中英

join - duplicates in nested lists

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.

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']]

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)]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM