我的代码有问题,我希望你能有答案。我有两个列表。
l1 = ['John','Rachel','Peter']
l2 = ['Rachel,'John','James']我希望有第三个列表作为输出,其中有一对夫妇的计数,其中约翰,瑞秋=瑞秋,约翰。所以像这样思考。约翰,瑞秋=2
瑞秋,约翰=2(最好的方法是忽略这一行,因为我们已经有了上面的计数)。
彼得,詹姆斯=1
这些信息是在一个数据框中,但我告诉自己,处理列表会更容易。告诉我你觉得这是不是个好主意。非常感谢你的回复。这是我的第一篇文章,英语不是我的母语。
发布于 2019-10-07 18:20:06
试试这个:
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})发布于 2019-10-07 17:23:49
你的问题还不完全清楚,但这会是一个开始吗?
import collections
collections.Counter(l1+l2)
# Counter({'Rachel': 2, 'John': 2, 'Peter': 1, 'James': 1})https://stackoverflow.com/questions/58266671
复制相似问题