加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

python3 判断元素是否在列表中

(2018-04-13 17:18:46)
分类: Python3
判断元素是否在列表中,可以通过in或者not in来判断。

代码实例:

fruit_basket = ['apple', 'banana', 'orange']
fruit = 'watermelon'
if fruit in fruit_basket:
    print(fruit + ", in the fruit_basket.")
else:
    print(fruit + ", not in the fruit basket.")

fruit = 'orange'
if fruit not in fruit_basket:
   
print(fruit + ", not in the fruit basket.")
else:
   
print(fruit + ", in the fruit_basket.")

输出:
watermelon, not in the fruit basket.
orange, in the fruit_basket.

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有