简体   繁体   English

比较两个列表中的元素并创建一个新列表

[英]Compare elements in two lists and make a new List

From two lists , make a new list, by comparing their i th position. 通过比较两个列表的第i个位置,从两个列表中创建一个新列表。 I can do it using loop but can't do using list comprehension 我可以使用循环来做,但不能使用列表理解

my code: 我的代码:

List1=[1,2,7,8]
List2=[3,4,5,6]
List3=[]
for i in range(len(List1)):
if(List1[i]>List2[i]):
   List3.append(List1[i])
else:
    List3.append(List2[i])

print(List3)

Desired output:[3,4,7,8] 所需的输出:[3,4,7,8]

使用邮编:

List3 = [max(x) for x in zip(List1, List2)]

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

相关问题 比较两个列表的元素 0; 使用 z[0] 中不在 x 中的项目创建新列表列表 - Compare element 0 of two lists of lists; make new list of lists with items in z[0] that are not in x 将列表元素与列表列表元素进行比较,并有条件地创建新列表 - compare elements of list to elements of list of lists and conditionally create new lists 如何比较两个具有相似元素的不同列表并使用循环创建没有相似元素的新列表 - How to compare two different lists with similar elements and create a new list without the similar elements using loops 比较两个列表的元素 - compare elements of two lists 将两个列表的相应元素合并到新列表 - Merging corresponding elements of two lists to a new list 将两个列表的总和作为新列表中的元素返回 - Return summation of two lists as elements in a new list 比较两个不同大小的列表列表并创建一个新列表 - Compare Two different size list of lists and create a new list Python:合并两个列表以创建一个新列表 - Python: Combining two lists to make a new list 如何比较 python 中的 N 个列表并创建具有唯一元素的新列表 - How to compare N lists in python and create a new list with unique elements 使用两个列表比较元素并在这两个列表中创建一个不同元素的列表? - Using two lists to compare elements and creating a list of non-same elements within those two lists?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM