Python 如何获得列表中两个值之间的差值

Python 如何获得列表中两个值之间的差值,python,list,set,set-difference,Python,List,Set,Set Difference,我试图得到temp1和temp2之间的差值,即10.25.60.156和10.22.17.180。由于temp2中的数据已包含在括号中,因此我一直收到此错误: z = set(temp1).symmetric_difference(set(temp2)) TypeError: unhashable type: 'list' 。 如果其中一个包含一个括号,我如何得到这两个之间的差异?提前谢谢 temp1 = ['10.25.39.70', '10.25.16.160', '10.25.60.15

我试图得到temp1和temp2之间的差值,即10.25.60.156和10.22.17.180。由于temp2中的数据已包含在括号中,因此我一直收到此错误:

z = set(temp1).symmetric_difference(set(temp2))
TypeError: unhashable type: 'list'
。 如果其中一个包含一个括号,我如何得到这两个之间的差异?提前谢谢

temp1 = ['10.25.39.70', '10.25.16.160', '10.25.60.156']
temp2 = [['10.25.16.160'], ['10.22.17.180'], ['10.25.39.70']]

z = set(temp1).symmetric_difference(set(temp2))
print(z)
印刷品:

{'10.22.17.180', '10.25.60.156'}
印刷品:

{'10.22.17.180', '10.25.60.156'}

为什么temp2的元素需要是列表?如果有,您可以使用列表理解在比较时选择temp2的第0个元素:

temp1 = ['10.25.39.70', '10.25.16.160', '10.25.60.156']
temp2 = [['10.25.16.160'], ['10.22.17.180'], ['10.25.39.70']]

z = set(temp1).symmetric_difference(set([x[0] for x in temp2))
print(z)

为什么temp2的元素需要是列表?如果有,您可以使用列表理解在比较时选择temp2的第0个元素:

temp1 = ['10.25.39.70', '10.25.16.160', '10.25.60.156']
temp2 = [['10.25.16.160'], ['10.22.17.180'], ['10.25.39.70']]

z = set(temp1).symmetric_difference(set([x[0] for x in temp2))
print(z)

请检查这是否适用于您:

temp1 = ['10.25.39.70', '10.25.16.160', '10.25.60.156']
temp2 = [['10.25.16.160'], ['10.22.17.180'], ['10.25.39.70']]

temp2= [item for sublist in temp2 for item in sublist]
print(set(temp1).symmetric_difference(temp2))

回答:{'10.22.17.180','10.25.60.156'}

请检查这是否适合您:

temp1 = ['10.25.39.70', '10.25.16.160', '10.25.60.156']
temp2 = [['10.25.16.160'], ['10.22.17.180'], ['10.25.39.70']]

temp2= [item for sublist in temp2 for item in sublist]
print(set(temp1).symmetric_difference(temp2))

回答:{'10.22.17.180','10.25.60.156'}

非常感谢!真是个救命恩人!非常感谢你!真是个救命恩人!