将元素从一个列表匹配到另一个列表,跳过已匹配的元素,Python
对你们中的许多人来说,解决这个问题的办法可能是显而易见的,但我被卡住了,所以我想我会问 我有以下格式的两个列表:将元素从一个列表匹配到另一个列表,跳过已匹配的元素,Python,python,list,text-processing,Python,List,Text Processing,对你们中的许多人来说,解决这个问题的办法可能是显而易见的,但我被卡住了,所以我想我会问 我有以下格式的两个列表: target_list =['apples 1', 'oranges 1', 'bananas 2', 'apples 3', 'oranges 2','mango 3', 'apples 2'] source_list = ['A apples', 'B mango', 'C apples', 'D bananas', 'E oranges','F apples', 'G ora
target_list =['apples 1', 'oranges 1', 'bananas 2', 'apples 3', 'oranges 2','mango 3', 'apples 2']
source_list = ['A apples', 'B mango', 'C apples', 'D bananas', 'E oranges','F apples', 'G oranges']
我需要遍历目标项列表中的每个目标项
,如果目标项()[0]
与源项列表中的源项()[1]
,返回目标项()[0],源项()[0],目标项()[1]
。重要的是输出中没有重复的源项/目标项对
这就是我的意思。假设我使用常规的旧for循环:
for target_item in target_list:
for source_item in source_list:
if source_item.split()[1] == target_item.split()[0]:
print target_item.split()[0], source_item.split()[0], target_item.split()[1]
我得到的(不正确)输出是:
apples A 1
apples C 1
apples F 1
oranges E 1
oranges G 1
bananas D 2
apples A 3
apples C 3
apples F 3
oranges E 2
oranges G 2
mango B 3
apples A 2
apples C 2
apples F 2
请注意,源/目标对apple A、apple C、apple F以不同的数字重复3次。橙子对也是如此。我需要的是
apples A 1
apples C 2
apples F 3
oranges E 1
oranges G 2
bananas D 2
mango B 3
即,每个条目应始终具有不同的源和目标
此外,对于每组“苹果$LETTER”和“橙色$LETTER”对,数字标签的排列是否不同并不重要。因此,以下是同样好的输出:
apples A 2
apples C 1
apples F 3
oranges E 2
oranges G 1
bananas D 2
mango B 3
target_list =['apples 1', 'oranges 1', 'bananas 2', 'apples 3', 'oranges 2','mango 3', 'apples 2']
source_list = ['A apples', 'B mango', 'C apples', 'D bananas', 'E oranges','F apples', 'G oranges']
from collections import defaultdict
# you want each target fruit to be a group, so use them as keys in a dict
# use a defaultdict list so whenever you access a key that doesn't exist
# it creates an empty list at that key
td = defaultdict(list)
for item in target_list:
key, value = item.split()
# the value for each fruit is a list of the numbers associated with it
td[key].append(value)
# for each source item find a match and pop a number from the list
# so that each pair gets a different number
for item in source_list:
letter, key = item.split()
if key in td:
print key, letter, td[key].pop()