Python 合并两个元组列表。包含两个元素的所需元组列表
我有两个元组列表:Python 合并两个元组列表。包含两个元素的所需元组列表,python,Python,我有两个元组列表: result=[(10002,),(10003,),(10004,),(10005,)…] result1=[('PL42222941176247135539240187',('PL81786645034401957047621964',),('PL61827884040081351674977449',)…] 我想要合并列表 所需输出: joined_result=[('PL42222941176247135539240187',10002,),('PL8178664503
result=[(10002,),(10003,),(10004,),(10005,)…]
result1=[('PL42222941176247135539240187',('PL81786645034401957047621964',),('PL61827884040081351674977449',)…]
我想要合并列表
所需输出:
joined_result=[('PL42222941176247135539240187',10002,),('PL81786645034401957047621964',10003,),('PL61827884040081351674977449',10004,)…]
我想到了zip,但有点错。
如何获得所需的输出?当您使用zip
进行迭代时,只需对单元素元组进行分解即可:
joined_result=[(x,y)表示zip中的((x,),(y,))(result1,result)]
使用zip进行迭代时,只需对单元素元组进行解构即可:
joined_result=[(x,y)表示zip中的((x,),(y,))(result1,result)]
似乎zip失败了,因为您有一个元组列表,而不仅仅是一个列表。
事实上,它创建了一个元组列表。
在传入zip之前转换元组列表:
result = [x[0] for x in result]
result1 = [x[0] for x in result1]
joined_result = list(zip(result1, result))
或者在一行中:
joined_result = list(zip([x[0] for x in result1],[x[0] for x in result]))
zip似乎失败了,因为您有一个元组列表,而不仅仅是一个列表。
事实上,它创建了一个元组列表。
在传入zip之前转换元组列表:
result = [x[0] for x in result]
result1 = [x[0] for x in result1]
joined_result = list(zip(result1, result))
或者在一行中:
joined_result = list(zip([x[0] for x in result1],[x[0] for x in result]))
使用:
警告!:它将中断result1,因此result2=result1将在循环之前的某个位置使用:
警告!:它将中断result1,result2=result1也会在循环之前的某个地方中断