Skip to main content
 首页 » 编程设计

python之将两个列表的相应元素合并到一个新列表

2025年05月04日15qq号

我有两个列表,如下所示,car_xy 和 car_id。我已经编写了一个示例代码来合并两个列表的相应元素,但没有得到所需的输出。

car_xy =[(650,700),(568,231),(789,123),(968,369)] 
car_id =[284,12,466,89] 
#required_details merges the two lists 
required_details = list(set(car_xy+car_id)) 
#now if i do print required_details the ouput will be a list like; 
required_details = [284,12,(650,700),89,(568,231),466,(968,369),(789,123)]  
#the required details adds the information in list randomly. What if i want  the first elements of both the list together, like 
required_details = [[284,(650,700)],[12,(568,231),[466,(789,123)],[89,(968,369)]] 

任何建议都会很棒。

请您参考如下方法:

实际上你需要[list(pair) for pair in zip(car_id, car_xy)]