我有元組的n個列表(N < 10),格式爲[(ListID,[(指數值),(索引值),...)],並希望通過索引排序他們得到以下結果合併N個分割元組的名單在python
Example Input:
[('A',[(0.12, 'how'),(0.26,'are'),(0.7, 'you'),(0.9,'mike'),(1.9, "I'm fine too")]),
('B',[(1.23, 'fine'),(1.50, 'thanks'),(1.6,'and you')]),
('C',[(2.12,'good'),(2.24,'morning'),(3.13,'guys')])]
Desired Output:
[('A', (0.12, 'how')),
('A', (0.26, 'are')),
('A', (0.7, 'you')),
('A', (0.9, 'mike')),
('B',(1.23, 'fine')),
('B',(1.50, 'thanks')),
('B',(1.6,'and you')),
('A', (1.9, "I'm fine too")),
('C',(2.12,'good')),
('C',(2.24,'morning')),
('C',(3.13,'guys'))]
我知道代碼是醜陋的,尤其是那些索引項[0] [ - 1] [1],但有人可以告訴我,我究竟做錯了什麼?
content = []
max = 0.0
first = True
Done = False
finished = []
while not Done:
for item in flow:
if len(finished) == 4:
Done = True
break
if len(item[1]) == 0:
if item[0] not in finished:
finished.append(item[0])
continue
if first == True:
max = item[1][-1][0]
content.append((item[0], item[1].pop()))
first = False
continue
if item[1][-1][0] > max:
max = item[1][-1][0]
content.append((item[0], item[1].pop()))
content = sorted(content, key=itemgetter(1))
first = True
UPDATE: 謝謝大家
Timsort利用部分排序的數據快速惡作劇。你的工作太多了。 – 2011-02-15 04:49:11
不要使用max作爲變量名,你可能希望能夠使用內建函數`max()`有一天 – 2011-02-15 04:50:13
這聽起來像你想要做的事很簡單,但是你給我們的所有東西都有些令人困惑輸出甚至不是python結構,也沒有示例輸入。例如在你的輸出中,列表ID都被分組在一起。 PLease只給出一些有效的Python數據結構來顯示輸入和所需的輸出 – 2011-02-15 04:57:58