简体   繁体   English

如果在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?

I am new here (and new to programming). 我是这里的新手(也是编程新手)。 I have tried to locate the relevant questions with no luck. 我试图找到相关的问题没有任何运气。

Basically, I got some lists shown below: 基本上,我得到了一些如下所示的列表:

[0, 'a', 1, 3, 3.3, 220, 22.27]
[0, 'b', 1, 13, 3.3, 220, 23.19]
[0, 'c', 1, 23, 3.3, 220, 24.11]
[1, 'a', 1, 3, 3.5, 200, 20.02]
[1, 'b', 1, 43, 3.3, 220, 25.94]
[2, 'a', 1, 3, 3.3, 250, 26.86]

I would like to use list items of indices 1 , 2 and 3 as the comparison key and search within all the lists available with the given key. 我想用索引列表项123是比较关键的,所有可用的定键的列表中进行搜索。

If found, then I need it the output to be made up of the list items of indices 0 , 4 , 5 and -1 . 如果找到了,那么我需要它的输出来弥补指数的列表项的045-1

So, with the given lists, the desired procedure is shown below. 因此,对于给定的列表,所需的过程如下所示。

First round: 第一回合:

  • comparison key : 'a' , 1 , 3 对比密钥: 'a' 13
  • go through the available lists 浏览可用列表
  • found the same key in 在找到相同的钥匙

    [0, 'a', 1, 3, 3.3, 220, 22.27] [0, 'a' ,1,3,3.3,220,22.27]

    [1, 'a', 1, 3, 3.5, 200, 20.02] [1, 'a' ,1、3、3.5、200、20.02]

    [2, 'a', 1, 3, 3.3, 250, 26.86] [2, 'a' ,1,3,3.3,250,26.86]

  • then create the output 然后创建输出

    0, 3.3, 220, 22.27 0、3.3、220、22.27

    1, 3.5, 200, 20.02 1,3.5,200,20.02

    2, 3.3, 250, 26.86 2,3.3,250,26.86

I have no idea how to code this yet, so could anyone help me please? 我还不知道该如何编码,所以有人可以帮我吗?

Thanks in advance 提前致谢

@wagaman: Hi Pal, Here a little snippet to do this job: @wagaman:嗨,朋友,这里有一些代码片段可以完成这项工作:

universe = []
universe.append([0, 'a', 1, 3, 3.3, 220, 22.27])
universe.append([0, 'b', 1, 13, 3.3, 220, 23.19])
universe.append([0, 'c', 1, 23, 3.3, 220, 24.11])
universe.append([1, 'a', 1, 3, 3.5, 200, 20.02])
universe.append([1, 'b', 1, 43, 3.3, 220, 25.94])
universe.append([2, 'a', 1, 3, 3.3, 250, 26.86])


def extract_from_lists(universe, keys=[1,2,3], matches=['a',1,3]):
    if len(keys) == len(matches):
        for l in universe:
            valid = True
            for i in range(len(keys)):
                if l[keys[i]] != matches[i]:
                    valid = False
                    break
            if valid:
                print("{},{},{},{}".format(l[0],l[4],l[5],l[-1]))
    else:
        print('Keys List and Matches List length must be equal.')


extract_from_lists(universe)

If you need further information about it, let me know, it's a very simple comparison and use basic concepts of python as default values of arguments, iteration and so. 如果您需要有关它的更多信息,请告诉我,这是一个非常简单的比较,并使用python的基本概念作为参数的默认值,迭代等。

Let us first define the input data: 让我们首先定义输入数据:

some_lists = [[0, 'a', 1, 3, 3.3, 220, 22.27],
              [0, 'b', 1, 13, 3.3, 220, 23.19],
              [0, 'c', 1, 23, 3.3, 220, 24.11],
              [1, 'a', 1, 3, 3.5, 200, 20.02],
              [1, 'b', 1, 43, 3.3, 220, 25.94],
              [2, 'a', 1, 3, 3.3, 250, 26.86]]

We have to iterate over the sublists of some_lists . 我们必须遍历some_lists的子列表。 To that end we will use an index variable, say x . 为此,我们将使用索引变量x For each sublist x we need to check whether x[1] == 'a' and x[2] == 1 and x[3] == 3 is fulfilled or not. 对于每个子列表x我们需要检查x[1] == 'a' and x[2] == 1 and x[3] == 3是否得到满足。 This logical expression can be simplified through slice notation to x[1:4] == ['a', 1, 3] . 可以通过将切片符号简化为x[1:4] == ['a', 1, 3]来简化此逻辑表达式。 For those x that match the condition, we create the desired output by selecting the specified items, namely [x[0], x[4], x[5], x[-1]] . 对于那些符合条件的x ,我们通过选择指定的项[x[0], x[4], x[5], x[-1]]创建所需的输出。 All this logic can be readily implemented through a list comprehension in just one line of code: 只需一行代码即可通过列表理解轻松实现所有这些逻辑:

In [253]: [[x[0], x[4], x[5], x[-1]] for x in some_lists if x[1:4] == ['a', 1, 3]]              
Out[253]: [[0, 3.3, 220, 22.27], [1, 3.5, 200, 20.02], [2, 3.3, 250, 26.86]]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM