I want to compare two list in a specific way which is scalable. I have thousands of entries.
- start with L1 index item1
- if it exists in L2 then check within that index if the alpha numeric of L1 exists in L2 . IF it does exist output the L1 values and add "yes" if found or "no" not found
List
L1 = [['item1', '3448ed1c4bb4'], ['item2', '6448ed1c4a2d'], ['item3', '8448ed1c4997,0048ed1c4997'], ['item4', 'b026282829e0,b02628b8cf36'], ['item5', 'b02628281bc0,b02628b99bc0'], ['item6', 'b02628281d40,b02628b9b156'], ['item7', '0050569bb9c2'], ['item8', '0050569a7b85'], ['item9', '3cecef5ee170,5254000fbd5a']]
L2 = [['item1', '3448ed1c4bb4'], ['item2', '3448ed1c4a2d'], ['item3', '6448ed1c4997'], ['item4', 'c026282829e0,b02628b8cf36'], ['item5', 'b02628281bc0,b02628b99bc0'], ['item6', 'b02628281d40,b02628b9b156,8948ed1c4997'], ['item7', '0050569bb9c2'], ['item8', '0050569a7b85'], ['item9', '3cecef5ee170,5254000fbd5a']]
Desire Result:
item1 3448ed1c4bb4 yes
item2 6448ed1c4a2d no
item3 8448ed1c4997 no
item3 0048ed1c4997 no
item4 b026282829e0 no
item4 b02628b8cf36 yes
etc
Code
L1 = [['item1', '3448ed1c4bb4'], ['item2', '6448ed1c4a2d'], ['item3', '8448ed1c4997,0048ed1c4997'], ['item4', 'b026282829e0,b02628b8cf36'], ['item5', 'b02628281bc0,b02628b99bc0'], ['item6', 'b02628281d40,b02628b9b156'], ['item7', '0050569bb9c2'], ['item8', '0050569a7b85'], ['item9', '3cecef5ee170,5254000fbd5a']]
L2 = [['item1', '3448ed1c4bb4'], ['item2', '3448ed1c4a2d'], ['item3', '6448ed1c4997'], ['item4', 'c026282829e0,b02628b8cf36'], ['item5', 'b02628281bc0,b02628b99bc0'], ['item6', 'b02628281d40,b02628b9b156,8948ed1c4997'], ['item7', '0050569bb9c2'], ['item8', '0050569a7b85'], ['item9', '3cecef5ee170,5254000fbd5a']]
if '3448ed1c4bb4' in L2:
print ("yes")
else:
print("no")
Should be yes.
Question:
- Is the only way to search for each item# to go through the entire L2 list using a loop?
CodePudding user response:
You can use zip
:
for (name1, alnums1), (_, alnums2) in zip(L1, L2):
alnums2 = set(alnums2.split(','))
for alnum in alnums1.split(','):
print(f"{name1} {alnum} {'yes' if alnum in alnums2 else 'no'}")
Output:
item1 3448ed1c4bb4 yes
item2 6448ed1c4a2d no
item3 8448ed1c4997 no
item3 0048ed1c4997 no
item4 b026282829e0 no
item4 b02628b8cf36 yes
item5 b02628281bc0 yes
item5 b02628b99bc0 yes
item6 b02628281d40 yes
item6 b02628b9b156 yes
item7 0050569bb9c2 yes
item8 0050569a7b85 yes
item9 3cecef5ee170 yes
item9 5254000fbd5a yes
CodePudding user response:
import re
L1 = [['item1', '3448ed1c4bb4'], ['item2', '6448ed1c4a2d'], ['item3', '8448ed1c4997,0048ed1c4997'], ['item4', 'b026282829e0,b02628b8cf36'], ['item5', 'b02628281bc0,b02628b99bc0'], ['item6', 'b02628281d40,b02628b9b156'], ['item7', '0050569bb9c2'], ['item8', '0050569a7b85'], ['item9', '3cecef5ee170,5254000fbd5a']]
L2 = [['item1', '3448ed1c4bb4'], ['item2', '3448ed1c4a2d'], ['item3', '6448ed1c4997'], ['item4', 'c026282829e0,b02628b8cf36'], ['item5', 'b02628281bc0,b02628b99bc0'], ['item6', 'b02628281d40,b02628b9b156,8948ed1c4997'], ['item7', '0050569bb9c2'], ['item8', '0050569a7b85'], ['item9', '3cecef5ee170,5254000fbd5a']]
for (item, pattern), (_, values) in zip(L1, L2):
for value in re.findall(r"\b\w \b", values):
print(item, value, "yes" if pattern == value else "no")