简体   繁体   English

如何将一个元素与列表中的其他元素进行比较

[英]How to compare one element with other elements within the list

I have a list x = [a, b, c, d] .我有一个列表x = [a, b, c, d]

I want to compare:我想比较:

  1. First element a with b , c and d .第一个元素abcd
  2. Second element b with a , c and d .第二个元素bacd
  3. Third element c with a , b and d .第三个元素cabd
  4. Fourth element d with a , b and c .第四个元素dabc

This is the method I have used but it's not working as I want:这是我使用的方法,但它没有按我想要的那样工作:

for i in len(x):
    for j in range(i+1, len(x)):
        Compare(x[i], y[j])
for i in range(len(x)):
    for el in x[:i] + x[(i+1):]:
        print("Compare {} with {}".format(x[i], el))
        # or: Compare(x[i], el)
"""
Output:
Compare a with b
Compare a with c
Compare a with d
Compare b with a
Compare b with c
Compare b with d
Compare c with a
Compare c with b
Compare c with d
Compare d with a
Compare d with b
Compare d with c
"""

As a function:作为一个功能:

def exclusive_compare(l, comparefun):
    return [comparefun(l[i], el) for el in l[:i] + l[(i+1):] for i in range(len(l))]

exclusive_compare(l, lambda x, y: (x, y))

Out[9]: 
[('a', 'b'),
 ('a', 'c'),
 ('a', 'd'),
 ('b', 'a'),
 ('b', 'c'),
 ('b', 'd'),
 ('c', 'a'),
 ('c', 'b'),
 ('c', 'd'),
 ('d', 'a'),
 ('d', 'b'),
 ('d', 'c')]

This looks like a good place to use itertools .这看起来是使用itertools的好地方。

for i in list(itertools.permutations(x, 2)):
    print("Compare {0} with {1}".format(*i))

"""    
Compare a with b
Compare a with c
Compare a with d
Compare b with a
Compare b with c
Compare b with d
Compare c with a
Compare c with b
Compare c with d
Compare d with a
Compare d with b
Compare d with c
"""

If a == b and b == a then you can use combinations to get distinct pairs:如果a == bb == a那么你可以使用组合来获得不同的对:

for i in list(itertools.combinations(x, 2)):
    print("Compare {0} with {1}".format(*i))

"""
Compare a with b
Compare a with c
Compare a with d
Compare b with c
Compare b with d
Compare c with d
"""

A silly mistake, you forgot to print hte comparison.一个愚蠢的错误,你忘了打印 hte 比较。 Here is the solution:这是解决方案:

For i in len(x):
 For j in range(i+1, len(x)):
  print(Compare(x[i], y[j]))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python:如何比较一个列表中的元素 - python: How to compare elements within one list Python:如何将列表的一个元素与除自身之外的所有其他元素进行比较? - Python: How do I compare one element of a list with every other elements besides itself? 如何将每个列表的每个元素与所有其他元素进行比较? - How to compare every element of every list to all the other elements? 如果在Python中发现重复项,如何使用列表中的某些元素比较其他列表中的元素并输出某个元素? - How to use certain elements in a list to compare elements in other lists and output a certain element if duplicates are found in Python? 将列表中的元素与同一列表中的所有其他元素进行比较 - compare elements in list to every other element in same list 如何比较空元素的列表元素? - How to compare list elements for empty element? 比较列表中的每个元素是否大于其他所有元素 - Compare if every element of a list is larger then every other elements 比较列表中列表的元素 - Compare elements of list within a list 如何将一个列表的元素与其他两个列表的元素进行比较,并为 python 中的每个元素生成 output? - How to compare the elements of one list with those of two others and generate an output for every element in python? Python:将列表中的一个元素分配给另一个列表中的元素 - Python: Assign one element from a list to elements in other list
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM