[英]Matching exact elements in a set where order doesn't matter
I'm new to python and I'm trying to match the exact elements between two sets, regardless of order. 我是python新手,无论顺序如何,我都试图匹配两组之间的确切元素。 So if my 2 sets are:
因此,如果我的2套是:
reflist = [1],[2,3,4],[5,6]
qlist = [1,2,3,4],[6,5]
The number of matches should be 1, which is 5,6 比赛次数应为1,即5,6
I tried to write the following loop to match the elements in qlist against reflist, and count the number of matches: 我尝试编写以下循环以将qlist中的元素与reflist进行匹配,并计算匹配数:
i = 0
count = 0
for each in qlist:
while i < len(qlist):
if each.split(",").sort == reflist[i].split(",").sort:
count = count + 1
i = i + 1
print count
However, I keep getting count = 0, even if the order of 5 and 6 in qlist is 5,6. 但是,即使qlist中的5和6的顺序是5,6,我也总是得到count = 0。 Would really appreciate any help with this!
非常感谢您的帮助!
This could do: 这可以做到:
If you have no duplicates: 如果没有重复项:
matches = [x for x in map(set, reflist) if x in map(set, qlist)]
If you have duplicates: 如果重复:
matches = [x for x in map(sorted, reflist) if x in map(sorted, qlist)]
If there are no duplicates in your "sets", convert your "sets" to a set of frozenset
s, and find the set intersection - 如果您的“集合”中没有重复项,请将“集合”转换为一组
frozenset
,然后找到该集合的交集 -
i = set(map(frozenset, reflist))
j = map(frozenset, qlist)
len(i.intersection(j))
1
You could always use collections.Counter()
for this: 您可以始终为此使用
collections.Counter()
:
from collections import Counter
reflist = [[1],[2,3,4],[5,6]]
qlist = [[1,2,3,4],[6,5]]
result = [list(x.keys()) for x in [Counter(y) for y in reflist] if x in [Counter(y) for y in qlist]]
print(result)
Which Outputs: 哪些输出:
[[5,6]]
Here is my one-liner, using frozenset
s and and
: 这是我的一线书,使用了
frozenset
和and
:
len(set(map(frozenset, qlist)) and set(map(frozenset, reflist)))
I understand you are new to Python, hence I will answer your question using your own method, just for the sake of recording the basic straightforward answer for future reference. 我了解您是Python的新手,因此我将使用您自己的方法回答您的问题,仅是为了记录基本的直接答案以供将来参考。
First of all, your code shouldn't run at all. 首先,您的代码根本不应该运行。 It must error out, because both
each
and reflist[i]
are lists, and you are applying a string method of split(",")
on them. 必须出错,因为
each
和reflist[i]
都是列表,并且您正在对它们应用split(",")
的字符串方法。 Therefore you are getting the initial value of count = 0
. 因此,您将获得
count = 0
的初始值。 You must check in the first place whether your code is even touching all the elements of qlist
and reflist
. 您必须首先检查代码是否甚至触及了
qlist
和reflist
所有元素。 This is not Code Review , hence I will leave it to you to run this and see the answer: 这不是Code Review ,因此我将它留给您来运行它并查看答案:
i = 0
count = 0
for each in qlist:
while i < len(qlist):
print i
print each
print reflist[i]
i = i + 1
Keep in mind: You don't have to iterate on index! 请记住: 您不必迭代索引! You can just loop over the elements of iterables directly!
您可以直接循环遍历可迭代元素! This is the answer you are looking for:
这是您要寻找的答案:
match = [] # Optional, to see all the matching elements
count = 0
for q in qlist:
for r in reflist:
if set(q) == set(r):
print q, r
match.append(q)
count += 1
print match
print count, len(match)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.