配对来自 2 个列表的项目
Pair items from 2 lists
我是 Python 的新手,我还在学习中。我需要一些帮助,了解如何配对两个列表中的项目,以及如果一个列表的项目多于另一个列表,如何实现自定义配对。
两个列表长度相同的示例:
List1 = [Drew, Ken]
List2 = [Ralph, Kevin]
输出应该是这样的:
Drew and Ralph
Ken and Kevin
另一个例子,如果一个列表有一个额外的项目:
List1 = [Drew, Ken, Mat]
List2 = [Ralph, Kevin]
输出应该是这样的:
Drew and Ralph
Ken and Kevin
Mat is alone
最后一个例子,如果一个列表有两个额外的项目:
List1 = [Drew, Ken, Mat, Jay]
List2 = [Ralph, Kevin]
输出应该是这样的:
Drew and Ralph
Ken and Kevin
Mat and Jay
匹配列表大小并压缩它们。
>>> l1 = ['Pedro', 'Rodrigues', 'Faded']
>>> l2 = ['Someone']
>>> for _ in range(abs(len(l1) - len(l2))):
... l2.append('No one')
...
>>> for who, with_who in zip(l1, l2):
... print(f'{who} is with {with_who}')
...
Pedro is with Someone
Rodrigues is with No one
Faded is with No one
>>>
使用 Zip 只能弥补您描述的第一种情况。但如果你只是想完成工作,那么你可以使用它。
list1 = ["Manish","Kalol","Haha","Ram","lala"]
list2 = ["lol","Hmmmm"]
if len(list1)>=len(list2):
biggerlist = list1
smallerlist = list2
else:
smallerlist = list1
biggerlist =list2
for i in range(len(smallerlist)):
print(biggerlist[i] + " with "+ smallerlist[i])
for i in range(len(smallerlist),len(biggerlist)-1 ,2):
print(biggerlist[i] + " with " + biggerlist[i+1])
if(not (len(biggerlist)-len(smallerlist))%2==0):
print(biggerlist[len(biggerlist)-1] + " is alone")
我认为这可以作为一个开始,我可以花一些时间以简单的方式改进它。
List1 = ['Drew', 'Ken']
List2 = ['Ralph', 'Kevin']
Listx = list(zip(List1, List2))
for item in Listx:
print(f'{item[0]} and {item[1]}')
让我们试试这个涵盖所有场景的解决方案,它是可扩展的并且保留每个列表中所有项目的顺序:
from itertools import chain, islice, zip_longest
def partners(l1, l2):
length = min(len(l1), len(l2)) # Number of common items
big_list = chain(*zip(l1, l2), islice(l1, length, None), islice(l2, length, None))
for who, partner in zip_longest(big_list, big_list):
print(f'{who} and {partner}' if partner else f'{who} is alone')
List1 = ['Drew', 'Ken']
List2 = ['Ralph', 'Kevin']
print("Matched Pairs")
partners(List1, List2)
List1 = ['Drew', 'Ken', 'Mat']
List2 = ['Ralph', 'Kevin']
print("\nUnmatched Pairs")
partners(List1, List2)
List1 = ['Drew', 'Ken', 'Mat', 'Jay']
List2 = ['Ralph', 'Kevin']
print("\nSuperfluous Pairs")
partners(List1, List2)
List1 = ['Drew']
List2 = []
print("\nSingle Player")
partners(List1, List2)
List1 = []
List2 = []
print("\nNobody")
partners(List1, List2)
Matched Pairs
Drew and Ralph
Ken and Kevin
Unmatched Pairs
Drew and Ralph
Ken and Kevin
Mat is alone
Superfluous Pairs
Drew and Ralph
Ken and Kevin
Mat and Jay
Single Player
Drew is alone
Nobody
其工作方式 big_list
包括以下内容:
- 从每个列表中压缩(配对)一个并展平该列表 (
*zip(l1, l2)
)
l1
中的项目列表不在 l2
中
l2
中的项目列表不在 l1
中
这导致 big_list 所有合作伙伴的通用顺序为 [a1, a2, b1, b2, c1, c2, ...] 其中相同的字母 = 合作伙伴。
然后我们使用 grouper()
食谱,让每个人都成为伙伴,并与任何未配对的 "No one"
伙伴一起循环它。
我是 Python 的新手,我还在学习中。我需要一些帮助,了解如何配对两个列表中的项目,以及如果一个列表的项目多于另一个列表,如何实现自定义配对。
两个列表长度相同的示例:
List1 = [Drew, Ken]
List2 = [Ralph, Kevin]
输出应该是这样的:
Drew and Ralph
Ken and Kevin
另一个例子,如果一个列表有一个额外的项目:
List1 = [Drew, Ken, Mat]
List2 = [Ralph, Kevin]
输出应该是这样的:
Drew and Ralph
Ken and Kevin
Mat is alone
最后一个例子,如果一个列表有两个额外的项目:
List1 = [Drew, Ken, Mat, Jay]
List2 = [Ralph, Kevin]
输出应该是这样的:
Drew and Ralph
Ken and Kevin
Mat and Jay
匹配列表大小并压缩它们。
>>> l1 = ['Pedro', 'Rodrigues', 'Faded']
>>> l2 = ['Someone']
>>> for _ in range(abs(len(l1) - len(l2))):
... l2.append('No one')
...
>>> for who, with_who in zip(l1, l2):
... print(f'{who} is with {with_who}')
...
Pedro is with Someone
Rodrigues is with No one
Faded is with No one
>>>
使用 Zip 只能弥补您描述的第一种情况。但如果你只是想完成工作,那么你可以使用它。
list1 = ["Manish","Kalol","Haha","Ram","lala"]
list2 = ["lol","Hmmmm"]
if len(list1)>=len(list2):
biggerlist = list1
smallerlist = list2
else:
smallerlist = list1
biggerlist =list2
for i in range(len(smallerlist)):
print(biggerlist[i] + " with "+ smallerlist[i])
for i in range(len(smallerlist),len(biggerlist)-1 ,2):
print(biggerlist[i] + " with " + biggerlist[i+1])
if(not (len(biggerlist)-len(smallerlist))%2==0):
print(biggerlist[len(biggerlist)-1] + " is alone")
我认为这可以作为一个开始,我可以花一些时间以简单的方式改进它。
List1 = ['Drew', 'Ken']
List2 = ['Ralph', 'Kevin']
Listx = list(zip(List1, List2))
for item in Listx:
print(f'{item[0]} and {item[1]}')
让我们试试这个涵盖所有场景的解决方案,它是可扩展的并且保留每个列表中所有项目的顺序:
from itertools import chain, islice, zip_longest
def partners(l1, l2):
length = min(len(l1), len(l2)) # Number of common items
big_list = chain(*zip(l1, l2), islice(l1, length, None), islice(l2, length, None))
for who, partner in zip_longest(big_list, big_list):
print(f'{who} and {partner}' if partner else f'{who} is alone')
List1 = ['Drew', 'Ken']
List2 = ['Ralph', 'Kevin']
print("Matched Pairs")
partners(List1, List2)
List1 = ['Drew', 'Ken', 'Mat']
List2 = ['Ralph', 'Kevin']
print("\nUnmatched Pairs")
partners(List1, List2)
List1 = ['Drew', 'Ken', 'Mat', 'Jay']
List2 = ['Ralph', 'Kevin']
print("\nSuperfluous Pairs")
partners(List1, List2)
List1 = ['Drew']
List2 = []
print("\nSingle Player")
partners(List1, List2)
List1 = []
List2 = []
print("\nNobody")
partners(List1, List2)
Matched Pairs
Drew and Ralph
Ken and Kevin
Unmatched Pairs
Drew and Ralph
Ken and Kevin
Mat is alone
Superfluous Pairs
Drew and Ralph
Ken and Kevin
Mat and Jay
Single Player
Drew is alone
Nobody
其工作方式 big_list
包括以下内容:
- 从每个列表中压缩(配对)一个并展平该列表 (
*zip(l1, l2)
) l1
中的项目列表不在l2
中
l2
中的项目列表不在l1
中
这导致 big_list 所有合作伙伴的通用顺序为 [a1, a2, b1, b2, c1, c2, ...] 其中相同的字母 = 合作伙伴。
然后我们使用 grouper()
食谱,让每个人都成为伙伴,并与任何未配对的 "No one"
伙伴一起循环它。