通过索引和名称匹配两个列表 [英] Match two lists by index and name

查看:88
本文介绍了通过索引和名称匹配两个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将两个列表进行比较,并创建一个输出列表,将常见项目移动到索引和名称匹配的位置.主列表创建一次,并且在整个脚本中保持不变.

How can I compare two lists together, and create an output list where common items are shifted to match in index and name. The main list is made once and stays the same throughout the script.

在某些情况下,更改列表将包含主列表中不存在的项目,我想为这些项目创建一个单独的列表...

There can be situations where the changing list will have items that do not exist in the main list, I'd like to create a separate list for these items...

示例:

main_list = ['apple', 'orange', 'banana', 'pear', 'mango', 'peach', 'strawberry']
changing_list = ['apple', 'banana', 'cucumber', 'peach', 'pear', 'fish']

output = ['apple', 'NA', 'banana', 'pear', 'NA', 'peach', 'NA']
added_output = ['cucumber', 'fish']

在比较之前在每个列表上使用sorted()函数可能会有些用处,但是,我无法直截了当地指出例如缺少橙色"(最好使用NA或X).我知道可以使用 集和&"运算符,但是,使用此符号并不能从索引/位置透视图(NA部分)中指示缺少哪个项目

Using the sorted() function on each list before comparison may be of some use, however, I can't get my head around indicating that 'orange', for example is missing (preferably by using NA or X). I am aware of the option of using, sets and the '&' operator, however, using this does not indicate which item was missing with an index/positioning perspective (the NA part)

推荐答案

您可以通过集合和列表理解来做到这一点:

You can do this with sets and list comprehensions:

def ordered_intersection(main_list, changing_list):
    changing_set = set(changing_list)
    output = [x if x in changing_set else 'NA' for x in main_list]

    output_set = set(output)
    added_output = [x for x in changing_list if x not in output_set]

    return output, added_output

其工作方式如下:

>>> main_list = ['apple', 'orange', 'banana', 'pear', 'mango', 'peach', 'strawberry']
>>> changing_list = ['apple', 'banana', 'cucumber', 'peach', 'pear', 'fish']
>>> ordered_intersection(main_list, changing_list)
(['apple', 'NA', 'banana', 'pear', 'NA', 'peach', 'NA'], ['cucumber', 'fish'])

上述代码的说明:

  • 首先将changing_list转换为集合,因为集合成员资格是固定时间,而列表成员资格是线性时间.
  • 由于我们要保持main_list的顺序进入输出,因此我们必须遍历该列表中的所有元素,并检查它们是否存在于changing_set中.这样可以避免每个操作的二次时间复杂度,并允许线性行为.
  • 以上逻辑也适用于added_output.
  • First convert changing_list to a set, since set membership is constant time, as opposed to list membership which is linear time.
  • Since we want to maintain the order of main_list into output, we have to traverse all the elements in that list, and check if they exist in changing_set. This prevents quadratic time complexity for each operation, and allows linear behavior instead.
  • The above logic is also applied to added_output.

这篇关于通过索引和名称匹配两个列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆