简体   繁体   English

如何在python中将一个列表中的项匹配到另一个列表?

[英]How can I match items from one list to the other in python?

I have a list called users2 which is a master list of users. 我有一个名为users2的列表,它是用户的主列表。 I have another list called codes has codes for each user in users2. 我有另一个名为代码的列表,其中包含users2中每个用户的代码。 The codes for the associated users are in the same order as in users2. 相关用户的代码与用户2的顺序相同。 So for example the code for bil is 56 because it is the second item in users2 and the second item in codes. 例如,bil的代码是56,因为它是users2中的第二项,代码中的第二项。

I also have another list called users1 that has some of the names from users2. 我还有另一个名为users1的列表,其中包含来自users2的一些名称。 I want to output of list of the associated codes for users1 based what the codes are for the same users in users2. 我想根据users2中相同用户的代码输出users1的相关代码列表。 So if the first item in user1 is bil, I want the first item in my output list to be 56.If users1 has a user name that is not in users2, the index in the output code list should be blank or "". 因此,如果user1中的第一项是bil,我希望输出列表中的第一项为56.如果users1的用户名不在users2中,则输出代码列表中的索引应为空或“”。 I need this in python. 我需要在python中使用它。

Here is what I have: 这是我有的:

users1=['bil','kim','john''tim']
users2['jim','bil','kim','tim''mike']
codes=['12''56','10','34','67']


for i in  range(len(users2)):
    for j in range(len (users1)):
        if users2[i]  == users1[j]:
            ordered.append(zipcodes[j])
print ordered

You are probably using the wrong data structure. 您可能使用了错误的数据结构。 You want users2 and codes to be combined into a dictionary. 您希望将users2codes组合成字典。

users1=['bil','kim','john','tim']
users2 = ['jim','bil','kim','tim','mike']
codes=['12','56','10','34','67']

users2_codes = dict(zip(users2, codes))
users1_codes = [users2_codes[user] if user in users2_codes else '' for user in users1 ]
print(users1_codes)

#>>> ['56', '10', '', '34']

You can construct a dictionary from users2 and codes and then loop through users1 and look up: 您可以从users2codes构造一个字典,然后循环遍历users1并查找:

[dict(zip(users2, codes)).get(u) for u in users1]
# ['56', '10', None, '34']

You can specify a different value for the missing user in the get method of dictionary: 您可以在字典的get方法中为缺少的用户指定不同的值:

[dict(zip(users2, codes)).get(u, "") for u in users1]
# ['56', '10', '', '34']

Just keep it nice and simple. 保持简洁美观。 A list and dict with a couple of loops solves the problem. 带有几个循环的列表和字典解决了这个问题。

users1=['bil','kim','john','tim']
users2 = ['jim','bil','kim','tim','mike']
codes=['12','56','10','34','67']

name_codes = {}
for key, value in zip(users2, codes):
    name_codes[key] = value

result = []
for user1 in users1:
    for user2 in name_codes:
        if user1 == user2:
            result.append(name_codes[user2])
    if user1 not in name_codes:
        result.append('')
print(result)

Output: 输出:

['56', '10', '', '34']

Update: 更新:

I fixed you're original code: 我修复了你的原始代码:

ordered = []
for user in users1:
    for i in range(len(users2)):
        if user == users2[i]:
            ordered.append(codes[i])
    if user not in users2:
        ordered.append('')
print(ordered)

Output: 输出:

['56', '10', '', '34']

You can try using dictionaries to store the users2 list and the codes list as a key:value pair. 您可以尝试使用词典将users2列表和代码列表存储为键:值对。 Then run a comparison between the users1 list and this list and print out the codes. 然后在users1列表和此列表之间进行比较并打印出代码。

code: 码:

    user2=dict()
    users2 = {'jim':'12','bil':'56','kim':'10','tim':'34','mike':'67'}
    users1=["bil",'kim','john','tim']
    ordered=list()

for i in users1:
 for j in users2:
    if i==j:
     ordered.append(users2[j])
 if i not in users2:
    ordered.append('')
print ordered

output: 输出:

    ['56','10','','34']

暂无
暂无

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

相关问题 如何在 Python 中轻松检查一个列表中的所有项目都包含在另一个列表中? - How can I easily check in Python that all items in one list are included in the other list? 如何比较列表中的一个项目与此列表中的所有其他项目python - how to compare one item in a list with all the other items in this list, python 如何使用Python从一个列表中选择单个项目并在第二个列表的所有项目上进行操作 - How can I select single item from one list and doing operation on all items of second list using Python 我想使用python中的列表理解一一打印列表中的项目。 怎么能这样做? - I want print items in list one by one using list comprehension in python. How can do that? 如何从 Python 中的列表中过滤项目? - How can I filter items from a list in Python? 如何从 for 循环 python 的列表中删除项目 - How can i delete items from list in for loop python 如何将两个列表项附加到一个里面有两个项目的项目? (Python) - How can I append two list items to one item with two items inside? (Python) 如何基于其他列表从python中的列表中删除项目 - How to remove the items from list in python based on other list 我如何从现有列表中创建一个列表(新),其中列表(新)的项目将在 python 中列出 - How can i create a list (new) from an existing list where items of list(new) will be list in python 如何从前一个x项是外部项的现有项创建嵌套列表? - How can I create a nested list from an existing one where the first x items are the outer items?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM