回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我在Python中有一个长长的列表,如下所示:</p>
<pre><code>myList=[
('a',[1,2,3,4,5]),
('b',[6,7,8,9,10]),
('c',[1,3,5,7,9]),
('d',[2,4,6,8,10]),
('e',[4,5,6,7,8])
]
</code></pre>
<p>我想详尽地列举一下这些共同的价值观</p>
^{pr2}$
<p>四、五、六人一组也一样,直到所有的共同值都被确定(假设列表更长)</p>
<p>是否可以使用<code>itertools</code>库或集合或以上两者的组合?在</p>
<p>我一直在尝试编写一个函数,在我生成的每一个新列表中循环原始列表,但效果并不理想!在</p>
<p>以下是我所拥有的:</p>
<pre><code>def findCommonElements(MyList):
def sets(items):
for name, tuple in items:
yield name, set(tuple)
def matches(sets):
for a, b in combinations(sets, 2):
yield ':'.join([a[0], b[0]]), a[1] & b[1]
combinationsSet=list(matches(sets(keywordCount)))
combinationsList=[]
for pair,tup in combinationsSet:
setList=list(tup)
combinationsList.<a href="https://www.cnpython.com/list/append" class="inner-link">append</a>((pair, len(setList), setList))
combinationsList=sorted(combinationsList,key=lambda x: x[1], reverse=True) #this just sorts the list by the number of common elements
return combinationsList
</code></pre>