如何在不使用Python中的Set的情况下在Tuple中查找缺失值和附加值?

时间:2018-11-03 10:53:11

标签: python logic

如何在不使用Set的情况下查找元组中的Missing和其他值。

例如,

tuple1= ("apple", "banana", "cherry")
tuple2= ("apple", "banana", "mango")

缺失值:樱桃
附加值:芒果

1 个答案:

答案 0 :(得分:1)

使用列表理解:

tuple1= ("apple", "banana", "cherry")
tuple2= ("apple", "banana", "mango")

# Missing:
print([x for x in tuple1 if x not in tuple2]) # ['cherry']

# Additional: 
print([x for x in tuple2 if x not in tuple1]) # ['mango']