列表开关故障

问题描述 投票:0回答:2

我的代码遇到麻烦,希望您能得到答复。我有两个清单。

l1 = ['John','Rachel','Peter']

l2 = ['Rachel,'John','James']

我想输出第三张列表,其中包含约翰·雷切尔(John,Rachel)=雷切尔·约翰(Rachel,John)的一对夫妇。这样的想法。约翰,瑞秋= 2

Rachel,John = 2(最好的方法是忽略此行,因为我们已经有了上面的计数)。

彼得,詹姆斯= 1

这些信息在数据框上,但我告诉自己,在列表上工作会更容易。告诉我您是否认为这是个好主意。非常感谢您的回答。这是我的第一篇文章,英语不是我的母语。

python-3.x
2个回答
0
投票

您的问题尚不完全清楚,但这会是一个开端吗?

import collections
collections.Counter(l1+l2)
# Counter({'Rachel': 2, 'John': 2, 'Peter': 1, 'James': 1})

0
投票

尝试这个:

l1 = ['John','Rachel','Peter']
l2=['Rachel','John','James']
import collections

l=[str(sorted([l1[i], l2[i]]) ) for i in range(min(len(l1), len(l2))) ]

print(collections.Counter(l))

输出:

Counter({"['John', 'Rachel']": 2, "['James', 'Peter']": 1})
© www.soinside.com 2019 - 2024. All rights reserved.