[英]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. 您希望将users2
和codes
组合成字典。
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: 您可以从users2
和codes
构造一个字典,然后循环遍历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.