<!DOCTYPE html>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS_HTML"></script> <!-- MathJax configuration --> <script type="text/x-mathjax-config"> MathJax.Hub.Config({ tex2jax: { inlineMath: [ ['$','$'], ["\\(","\\)"] ], displayMath: [ ['$$','$$'], ["\\[","\\]"] ], processEscapes: true, processEnvironments: true }, // Center justify equations in code and markdown cells. Elsewhere // we use CSS to left justify single line equations in code cells. displayAlign: 'center', "HTML-CSS": { styles: {'.MathJax_Display': {"margin": 0}}, linebreaks: { automatic: true } } }); </script> <!-- End of mathjax configuration --></head>
要检查特定的值是否包含在序列中,可以使用in关键字进行判断,下面针对字符串,列表,字典常用序列进行操作¶
In [7]:
'a' in 'abc'
Out[7]:
针对列表进行判断时的误区¶
In [8]:
'a' in ['a','b','c']
Out[8]:
In [9]:
'a' in ['ab','b','c']
Out[9]:
可以看到,列表中虽然有a,但ab才是列表中的元素,所以给出false
针对于字典的误区¶
In [10]:
'a' in {'a':'name'}
Out[10]:
In [11]:
'a' in {'name':'a'}
Out[11]:
由此可以知道,在对字典进行判断是,只会判断字典中的key值,不会判断字典中的value值
要解决特定值 非列表,字典成员,但存在于序列中是,可以先将序列转换成str
In [ ]:
In [13]:
'a' in str(['ab','b','c'])
Out[13]:
In [14]:
'a' in str({'name':'a'})
Out[14]:
新颖用法¶
In [15]:
input('please input :') in 'abc'
Out[15]:
来源:https://www.cnblogs.com/zhongzhouyun/p/12256222.html