我想比較具有相同長度但內容不同的列表的列表。我的腳本應該只返回職位共享完全相同的元素(在所有列表中)。 例如:比較等長列表以找到共享相同元素的位置
l = [[1,2,3,4,5,6,7,8],[9,8,8,4,3,4,5,7,8],[ 5,6,7,4,9,9,9,8],[0,0,1,4,7,6,3,8]]
因此,我得到一個位置列表p = [3,7]在所有列表中,我們分別在位置3和7處具有'4'和'8'。
這些元素也可以是字符串,我只是用整數給出一個例子。謝謝你的幫助!
我想比較具有相同長度但內容不同的列表的列表。我的腳本應該只返回職位共享完全相同的元素(在所有列表中)。 例如:比較等長列表以找到共享相同元素的位置
l = [[1,2,3,4,5,6,7,8],[9,8,8,4,3,4,5,7,8],[ 5,6,7,4,9,9,9,8],[0,0,1,4,7,6,3,8]]
因此,我得到一個位置列表p = [3,7]在所有列表中,我們分別在位置3和7處具有'4'和'8'。
這些元素也可以是字符串,我只是用整數給出一個例子。謝謝你的幫助!
l = [[1,2,3,4,5,6,7,8],[9,8,8,4,3,4,5,7,8],[5,6,7,4,9,9,9,8],[0,0,1,4,7,6,3,8]]
p = [i for i, j in enumerate(zip(*l)) if all(j[0]==k for k in j[1:])]
# p == [3] - because of some typo in your original list, probably too many elements in the second list.
這是這只是一個班輪(列表解析)版本,更詳細:
p = []
for i, j in enumerate(zip(*l)):
if all(j[0]==k for k in j[1:]):
p.append(i)
zip(*l)
爲您提供:
[(1, 9, 5, 0),
(2, 8, 6, 0),
(3, 8, 7, 1),
(4, 4, 4, 4),
(5, 3, 9, 7),
(6, 4, 9, 6),
(7, 5, 9, 3),
(8, 7, 8, 8)]
enumerate()
放數字0,1, 2,...到該列表中的每個元組。
all(j[0]==k for k in j[1:])
元組的第一個元素與所有剩餘的元素進行比較,並且返回True
如果它們全部都相等,False
否則(它一旦它找到一個不同的元素返回False
,所以它的更快)
l = [[1,2,3,4,5,6,7,8],[9,8,8,4,3,4,5,7,8],[5,6,7,4,9,9,9,8],[0,0,1,4,7,6,3,8]]
r = []
for i in range(len(l[0])):
e = l[0][i]
same = True
for j in range(1, len(l)):
if e != l[j][i]:
same = False
break
if same:
r.append(i)
print r
僅打印[3],因爲l [1]在位置7沒有8。它還有一個元素。
我喜歡eumiro解決方案,但我做了一組
p = [i for i, j in enumerate(zip(*l)) if len(set(j)) == 1]
+1,哇我忙於'all()'解決方案,我完全忘記了'set()'。雖然這比我的解決方案慢(我的測試只要一個元素不匹配就停下來),但它看起來很棒! – eumiro 2011-04-17 06:54:56
li = [[1,2,3,4,5,6,7,8],[9,8,8,4,3,6,5,8],[5,6,7,4,9,9,9,8],[0,0,1,4,7,6,3,8]]
first = li[0]
r = range(len(first))
for current in li[1:]:
r = [ i for i in r if current[i]==first[i]]
print [first[i] for i in r]
結果
[4, 8]
。
比較執行的時間:
from time import clock
li = [[1,2,3,4,5,6,7,8,9,10],
[9,8,8,4,5,6,5,8,9,13],
[5,6,7,4,9,9,9,8,9,12],
[0,0,1,4,7,6,3,8,9,5]]
n = 10000
te = clock()
for turn in xrange(n):
first = li[0]
r = range(len(first))
for current in li[1:]:
r = [ i for i in r if current[i]==first[i]]
x = [first[i] for i in r]
t1 = clock()-te
print 't1 =',t1
print x
te = clock()
for turn in xrange(n):
y = [j[0] for i, j in enumerate(zip(*li)) if all(j[0]==k for k in j[1:])]
t2 = clock()-te
print 't2 =',t2
print y
print 't2/t1 =',t2/t1
print
結果
t1 = 0.176347273187
[4, 8, 9]
t2 = 0.579408755442
[4, 8, 9]
t2/t1 = 3.28561221827
。
隨着
li = [[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,2,22,26,24,25],
[9,8,8,4,5,6,5,8,9,13,18,12,15,14,15,15,4,16,19,20,2,158,35,24,13],
[5,6,7,4,9,9,9,8,9,12,45,12,4,19,15,20,24,18,19,20,2,58,23,24,25],
[0,0,1,4,7,6,3,8,9,5,12,12,12,15,15,15,5,3,14,20,9,18,28,24,14]]
結果
t1 = 0.343173188632
[4, 8, 9, 12, 15, 20, 24]
t2 = 1.21259110432
[4, 8, 9, 12, 15, 20, 24]
t2/t1 = 3.53346690385
我們不是寫代碼爲您服務。你有什麼嘗試? – martineau 2011-04-15 16:31:19