考慮:
>>> tuple1=('H', 'C', 'G')
>>> tuple2=('H', 'M', 'G')
>>> tuple3=('L', 'M', 'S')
OK,你的國家,「我的要求是比較與其他記錄每一個記錄,並給予其不同的元素的屬性名稱。」
它放入代碼:
>>> [i for i, t in enumerate(zip(tuple1, tuple2), 1) if t[0]!=t[1]]
[2]
>>> [i for i, t in enumerate(zip(tuple1, tuple3), 1) if t[0]!=t[1]]
[1, 2, 3]
>>> [i for i, t in enumerate(zip(tuple2, tuple3), 1) if t[0]!=t[1]]
[1, 3]
那麼你的狀態「,最終輸出應爲{{Col2},{Col1,Col2,Col3},{Col1,Col3}}
因爲一套套將失去秩序,這是沒有意義的。它應該是:
>>> [[i for i, t in enumerate(zip(*pair), 1) if t[0]!=t[1]] for pair in
... [(tuple1, tuple2), (tuple1, tuple3), (tuple2, tuple3)]]
[[2], [1, 2, 3], [1, 3]]
如果你真的想套,你可以讓他們的子元素;如果你有一套真正的套件,你就失去了哪對的信息。
套清單:
>>> [{i for i, t in enumerate(zip(*pair), 1) if t[0]!=t[1]} for pair in
... [(tuple1, tuple2), (tuple1, tuple3), (tuple2, tuple3)]]
[set([2]), set([1, 2, 3]), set([1, 3])]
而且你幾乎相同的期望輸出:
>>> [{'Col{}'.format(i) for i, t in enumerate(zip(*pair), 1) if t[0]!=t[1]} for pair in
... [(tuple1, tuple2), (tuple1, tuple3), (tuple2, tuple3)]]
[set(['Col2']), set(['Col2', 'Col3', 'Col1']), set(['Col3', 'Col1'])]
(注意,由於集合是無序的,的字符串順序有所不同。如果頂層。訂單變更,你有什麼?)
注意,如果你有一個列表的列表,你是更接近你想要的輸出:
>>> [['Col{}'.format(i) for i, t in enumerate(zip(*pair), 1) if t[0]!=t[1]] for pair
... in [(tuple1, tuple2), (tuple1, tuple3), (tuple2, tuple3)]]
[['Col2'], ['Col1', 'Col2', 'Col3'], ['Col1', 'Col3']]
編輯基於評論
你可以做類似的東西:
def pairs(LoT):
# for production code, consider using a deque of tuples...
seen=set() # hold the pair combinations seen
while LoT:
f=LoT.pop(0)
for e in LoT:
se=frozenset([f, e])
if se not in seen:
seen.add(se)
yield se
>>> list(pairs([('H', 'C', 'G'), ('H', 'M', 'G'), ('L', 'M', 'S')]))
[frozenset([('H', 'M', 'G'), ('H', 'C', 'G')]), frozenset([('L', 'M', 'S'), ('H', 'C', 'G')]), frozenset([('H', 'M', 'G'), ('L', 'M', 'S')])]
然後可以這樣使用:
>>> LoT=[('H', 'C', 'G'), ('H', 'M', 'G'), ('L', 'M', 'S')]
>>> [['Col{}'.format(i) for i, t in enumerate(zip(*pair), 1) if t[0]!=t[1]] for pair
... in pairs(LoT)]
[['Col2'], ['Col1', 'Col2', 'Col3'], ['Col1', 'Col3']]
編輯#2
如果你想有一個頭VS的計算值:
>>> theader=['tuple col 1', 'col 2', 'the third' ]
>>> [[theader[i] for i, t in enumerate(zip(*pair)) if t[0]!=t[1]] for pair
... in pairs(LoT)]
[['col 2'], ['tuple col 1', 'col 2', 'the third'], ['tuple col 1', 'the third']]
如果你想(我懷疑的右答案)清單列表:
>>> di=[]
>>> for pair in pairs(LoT):
... di.append({repr(list(pair)): [theader[i] for i, t in enumerate(zip(*pair)) if t[0]!=t[1]]})
>>> di
[{"[('H', 'M', 'G'), ('H', 'C', 'G')]": ['col 2']}, {"[('L', 'M', 'S'), ('H', 'C', 'G')]": ['tuple col 1', 'col 2', 'the third']}, {"[('H', 'M', 'G'), ('L', 'M', 'S')]": ['tuple col 1', 'the third']}]
或者,只是列出的直快譯通:
>>> di={}
>>> for pair in pairs(LoT):
... di[repr(list(pair))]=[theader[i] for i, t in enumerate(zip(*pair)) if t[0]!=t[1]]
>>> di
{"[('H', 'M', 'G'), ('L', 'M', 'S')]": ['tuple col 1', 'the third'], "[('L', 'M', 'S'), ('H', 'C', 'G')]": ['tuple col 1', 'col 2', 'the third'], "[('H', 'M', 'G'), ('H', 'C', 'G')]": ['col 2']}
它看起來像你理解了要求寫。現在嘗試編寫一個解決方案,當你遇到一個你無法解決的問題時,一個你不能解決的問題 - 在這裏發佈。 – alfasin 2014-09-03 02:57:43
我試圖編寫代碼並最終編寫重複的循環,因此需要時間,因此需要尋找更好的替代方案。 – 2014-09-03 02:58:56
嘗試編寫解決問題所需的算法(步驟)。然後爲每個步驟編寫一個函數。在每個步驟單獨運行後,嘗試將這些步驟組合到一個工作流程中:按順序從主函數調用每個步驟。試圖將所有登錄「吞下」到一個巨大的函數/邏輯中對於閱讀,調試,測試和維護都是不利的(正如你已經經歷的那樣)。 – alfasin 2014-09-03 03:02:26