简体   繁体   English

匹配顺序无关紧要的集合中的确切元素

[英]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 : 这是我的一线书,使用了frozensetand

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. 必须出错,因为eachreflist[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 . 您必须首先检查代码是否甚至触及了qlistreflist所有元素。 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.

相关问题 Multiples-keys字典,其中键顺序无关紧要 - Multiples-keys dictionary where key order doesn't matter 2个坐标的快速哈希,顺序无关紧要? - Fast hash for 2 coordinates where order doesn't matter? 二维字典或其他数据结构,其中键的顺序无关紧要 - 2D Dictionary or other data structure where order of keys doesn't matter Python 3.x:如何比较包含字典的两个列表,其中顺序无关紧要 - Python 3.x: How to compare two lists containing dictionaries where order doesn't matter 熊猫:在顺序无关紧要的子集中查找重复项并将其合并 - Pandas: Find duplicates in subset where order doesn't matter and combine them 当我在集合中切换订单时,为什么我的代码采用不同的值(知道订单与集合无关) - Why does my code take different values when i switch the order in a set (knowing that order doesn't matter with sets) (pandas)如何根据三个相似的数据列创建唯一标识符,其中顺序无关紧要? - (pandas)How can I create a unique identifier based on three similar columns of data, where order doesn't matter? 正则表达式存在一些其顺序无关紧要的单词 - Regex for existence of some words whose order doesn't matter 交叉连接/合并以创建组合的数据框(顺序无关紧要) - cross join/merge to create dataframe of combinations (order doesn't matter) 在顺序无关紧要的情况下为网络构建适当的SQL查询 - Building the appropriate SQL queries for a web from, when order doesn't matter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM