简体   繁体   English

如何匹配两个不同列表中的项目并根据 python 中的匹配项创建新列表?

[英]How to match items in two different lists and create a new list based on the matches in python?

I have a list in python called "multiple_ids" with a bunch of ids and I have another list called "ids_singular" as well as another list called "alias".我在 python 中有一个名为“multiple_ids”的列表,其中包含一堆 id,我还有另一个名为“ids_singular”的列表以及另一个名为“alias”的列表。

"ids_singular" and "alias" are both the same size and the index of "ids_singular" corresponds to the index of "alias". “ids_singular”和“alias”的大小相同,“ids_singular”的索引对应“alias”的索引。 What this means is that say the third value in the "alias" list is another way to represent the third value in "ids_singular".这意味着说“别名”列表中的第三个值是表示“ids_singular”中第三个值的另一种方式。

The list "miltiple_ids" is larger than the other two lists and includes all values in "ids_singular', but there are duplicates as well. Every id in "mutiple_ids" can be found in "ids_singular".列表“miltiple_ids”比其他两个列表大,包括“ids_singular”中的所有值,但也有重复。“mutiple_ids”中的每个id都可以在“ids_singular”中找到。

What I am looking to do is for code that will replace each item (id) in "multiple_ids" with the matching alias from the "alias" list based on the "ids_singular" list.我要做的是使用基于“ids_singular”列表的“别名”列表中的匹配别名替换“multiple_ids”中的每个项目(id)的代码。

I have tried a double for loop where I first iterate through all the "multiple_ids", then iterate through all the "ids_singular" and if they are a match, create a new list that has the alias for the id based on the same index of "alias" list.我尝试了一个双循环,我首先遍历所有“multiple_ids”,然后遍历所有“ids_singular”,如果它们匹配,则创建一个新列表,该列表具有基于相同索引的 id 别名“别名”列表。

for i in (multiple_ids):
    for j in range(len(ids_singular)):
        if i==ids_singular[j]:
            new_multiple_ids.append(alias[j])
print(new_multiple_ids)

When I run this code, nothing happens当我运行此代码时,没有任何反应

I believe this is what you want:我相信这就是你想要的:

multiple_ids = ['abc', 'def', 'xyz', 'def', 'xyz']
ids_singular = ['abc','def','xyz']
alias = ['a_abc','a_def', 'a_xyz']

d = dict(zip(ids_singular, alias))
result = [d[item] for item in multiple_ids]

print(result)  $ -> ['a_abc', 'a_def', 'a_xyz', 'a_def', 'a_xyz']

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

相关问题 在 Python 中,如果您有两个共享多个元素的列表,您如何创建具有匹配项的新列表? - In Python, if you have two lists that share a number of elements, how do you create a new list with the matches? 如何比较两个列表中的项目并根据结果创建一个新列表? - How to compare items in two lists and create a new list out of results? 如何找到两个列表之间的匹配并根据匹配写入输出? - How to find the match between two lists and write the output based on matches? 根据其他两个列表在 Python 中创建列表? - Create list in Python based on two other lists? 当两个列表都基于索引匹配时,如何解码列表并从两个列表中删除项目? - How to decode a list and remove items from two lists when there is a match in both of them based on an index? 将基于其项目的两个列表列表映射到Python中的列表对 - Mapping two list of lists based on its items into list pairs in Python 比较两个不同大小的列表列表并创建一个新列表 - Compare Two different size list of lists and create a new list python匹配列表中的项目以创建新列表 - python matching up items in a list to create new lists 从Python列表中的项目创建新的空列表? - Create new empty lists from items within a Python list? 如何基于两个因素在 Python 中创建新列表? - How to create a new list in Python based on two factors?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM