我有以下陣列:如何分組這個python列表?
[1 , 2]
[1 , 3]
[1 , 4]
[2 , 3]
[2 , 4]
[5 , 1]
我想打印類似以下的輸出:
"Items related to 1:"
2, 3, 4, 5 *note this last one was on the first column and 1 in the second
"Items related to 2:
3, 4
"Items related to 3:"
1, 2
"Items related to 4:"
1, 2
"Items related to 5:"
1
任何想法?
我寧願'd = defaultdict(套)',這樣你就可以擺脫在循環中的兩個'setdefault()'調用。 – raymonad
@raymonad:好的,謝謝!我會編輯它。 –
謝謝,像奇蹟般的作品! – user2091936