python in和not in 在list中可以用吗

作者&投稿:紫水 (若有异议请与网页底部的电邮联系)
Python中关系运算符in,not in在字符串表达式和列表的使用时有什么区别和注意点?~

Membership test operations
For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expression x in y is equivalent to any(x is e or x == e for e in y).
For the string and bytes types, x in y is True if and only if x is a substring of y. An equivalent test is y.find(x)!= -1. Empty strings are always considered to be a substring of any other string, so "" in "abc" will return True.
翻译:
对容器类型,例如list、tuple、set、frozenset、dict或collections.deque,表达式x in y等价于any(x is e or x == e for e in y)。
对字符串和bytes类型,x in y为真当且仅当x是y的子串。等价测试为y.find(x) != -1。空字符串永远被视作是其他任何字符串的子集,因此"" in "abc"将返回True。

name=''while not name:

name=raw_input(u'请输入姓名:')
print name

python中的not具体表示是什么:
在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法:
(1) not与逻辑判断句if连用,代表not后面的表达式为False的时候,执行冒号后面的语句。比如:
a = False
if not a: (这里因为a是False,所以not a就是True)
print "hello"
这里就能够输出结果hello
(2) 判断元素是否在列表或者字典中,if a not in b,a是元素,b是列表或字典,这句话的意思是如果a不在列表b中,那么就执行冒号后面的语句,比如:
a = 5
b = [1, 2, 3]
if a not in b:
print "hello"
这里也能够输出结果hello

自然是可以的.元组列表都是可以用的.
Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange
在上面这些类型里面都是可以使用in/not in的.


西峡县18282962471: python中如何使用not in -
字翔盐酸: name=''while not name: name=raw_input(u'请输入姓名:') print namepython中的not具体表示是什么: 在python中not是逻辑判断词,用于布尔型True和False,not True为False,not False为True,以下是几个常用的not的用法: (1) not与逻辑判...

西峡县18282962471: python in和not in 在list中可以用吗 -
字翔盐酸: 自然是可以的.元组列表都是可以用的. Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange 在上面这些类型里面都是可以使用in/not in的.

西峡县18282962471: python enumerate跟直接in有什么区别 -
字翔盐酸: 两个区别很明显: https://docs.python.org/2/library/functions.html#enumerate1 2 3 4 5defenumerate(sequence, start=0):n =startforelem insequence:yieldn, elemn +=1 首先看enumerate返回的是个iterator in的话参考文档:(下面摘录)...

西峡县18282962471: python中的if in是什么意思 -
字翔盐酸: if条件语句后面需要跟随bool类型的数据,即True或者False.然而,如果不是bool类型的数据,可以将其转换成bool类型的数据,转换的过程是隐式的. 在Python中,None、空列表[]、空字典{}、空元组()、0等一系列代表空和无的对象会被转换成False.除此之外的其它对象都会被转化成True. 在命令if not 1中,1便会转换为bool类型的True.not是逻辑运算符非,not 1则恒为False.因此if语句if not 1之下的语句,永远不会执行.

西峡县18282962471: python的列表,有没有查找的功能 -
字翔盐酸: 如楼上,列表就是用索引,list.index('x'), 或者你就是查找某个值的话就用for循环如下: for x in list:print x;

西峡县18282962471: Python 中 not 'x' in 'sex' 和 'x' not in 'sex' 有区别吗 -
字翔盐酸: 'x' not in 'sex' : 'x'不在'sex'里,不在的话返回true,在返回false not 'x' in 'sex' : 先判断'x'在'sex',然后取not. 其实两个结果一样,就是结构上就是not的先后顺序有点不一样.

西峡县18282962471: python 发现列表包含指定内容则退出循环 -
字翔盐酸: f1是个列表 如果没有其他需要的话可以不循环 直接写 if a1 not in f1: exit()

西峡县18282962471: 在Python中怎么匹配多个“取非”的长字符串 -
字翔盐酸: 按照你的要求写的正则表达式 ^((?!ABC)(?!一二三).)+$ 完整的Python程序如下 import re s=['123ABC12345','12一二三12345','1一二345','12A345678','1一2二3三45','1AB23C45'] regex = r'^((?!ABC)(?!一二三).)+$' for i in range(0,len(s)): result = re.match(regex,s[i]) if result: print(result.group(0))Python源代码(注意源代码的缩进) 运行结果

西峡县18282962471: python 中the label is not in the 是什么意思 -
字翔盐酸: python 中the label is not in the Python中标签不在python 中the label is not in the Python中标签不在

西峡县18282962471: python使用in操作符时元组和数组的区别分析 -
字翔盐酸: 在python中可以使用in符号判断指定的元素是否存在于列表中,但我发现元组和数组存在区别,下面是详细实验结果.>>> 'test' in ['replace','test'] True>>> 'test' in ('replace','test') True>>> 'test' in ['test/codes','replace'] False>>> 'test' in ('test/codes','...

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网