goodslist=[['Iphone', 5800], ['Mac Pro', 12000], ['Bike', 800], ['Alex Python', 89], ['Starbuck Latte', 30], ['Cap', 45], ['Air conditioning', 3000], ['Ipad', 4200]]
假设我想判断Bike是否存在于该列表里面,该怎么做?
1.我自己一开始的想法是 ‘Bike’ in goodslist[:][0],发现是行不通的这样子效果就等于 goodslist[0]
2.另外的思路我想是用for循环,一个一个判断过去,但是这样子比较麻烦,求问大神们有什么‘还有这种操作’的思路
第一种方法:
goodslist = [['Iphone', 5800], ['Mac Pro', 12000], ['Bike', 800], ['Alex Python', 89], ['Starbuck Latte', 30], ['Cap', 45], ['Air conditioning', 3000], ['Ipad', 4200]] goods = [x for x in goodslist if 'Bike' in x[0]] if len(goods) > 0: print('Bike存在goodslist') else: print('Bike不存在goodslist')
第二种方法:
goodslist = [['Iphone', 5800], ['Mac Pro', 12000], ['Bike', 800], ['Alex Python', 89], ['Starbuck Latte', 30], ['Cap', 45], ['Air conditioning', 3000], ['Ipad', 4200]] goods = filter(lambda x: 'Bike' == x[0], goodslist) print(list(goods))