pythonnotinlist
‘壹’ python的列表,有没有查找的功能
Python中是有查找功能的,四种方式:in、not in、count、index,后两种方式是列表的方法,下面以a_list = ['a','b','c','hello'],为例作介绍:
判断值是否在列表中,in操作符:
#判断值a是否在列表中,并返回True或False
'a'ina_lis判断值是否不在列表,not in操作符:
#判断a是否不在列表中,并返回True或False
'a'notina_list统计指定值在列表中出现的次数,count方法:
#返回a在列表中的出现的次数
a_list.count('a')查看指定值在列表中的位置,index方法:
#返回a在列表中每一次出现的位置,默认搜索整个列表
a_list.index('a')
#返回a在指定切片内第一次出现的位置
a_list.index('a',0,3)
‘贰’ python中remove "x not in list"怎么处理
remove之前先判断一下
num = 2
if num in a:
a.remove(num )
‘叁’ Python中关系运算符in,not in在字符串表达式和列表的使用时有什么区别和注意点
Membership test operations
For container types such as list, tuple, set, frozenset, dict, or collections.deque, the expressionxinyis equivalent toany(xiseorx==eforeiny).
For the string and bytes types,xinyisTrueif and only ifxis a substring ofy. An equivalent test isy.find(x)!=-1. Empty strings are always considered to be a substring of any other string, so""in"abc"will returnTrue.
翻译:
对容器类型,例如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。
‘肆’ Python :一个for循环无法实现,出现“x not in list”,菜鸟一个,请问大神怎么回事
a=[1,3,3,4,5,6,3343,52]
ad=[]
importrandom
foriinrange(len(a)):#range(8),i=[0,1,2,3,4,5,6,7]
print(random.choice(a))
ad=a.remove(i)#wheni=2,inotina,Error!
print(ad)
print(a)
‘伍’ python中的not具体表示是什么,举个例子说一下,衷心的感谢
在python中not是逻辑判断词,用于布尔型True和False。
布尔"非" :如果 x 为 True,返回 False 。如果 x 为 False,它返回 True。 例如:
a = 0;
b = 1;
if not ( a and b ):
print "变量 a 和 b 都为 false,或其中一个变量为 false";
else:
print "变量 a 和 b 都为 true";
输出结果为:变量 a 和 b 都为 false,或其中一个变量为 false。
(5)pythonnotinlist扩展阅读
1、not 和 in 连接的用法:
not in ,如果在指定的序列中没有找到值返回 True,否则返回 False。x 不在 y 序列中 , 如果 x 不在 y 序列中返回 True。例如:
b = 20;
list = [1, 2, 3, 4, 5 ];
if ( b not in list ):
print "变量 b 不在给定的列表中 list 中";
else:
print "变量 b 在给定的列表中 list 中";
2、is 和 not 连接的用法:
is not , 是判断两个标识符是不是引用自不同对象,x is not y, 类似id(a) != id(b)。如果引用的不是同一个对象则返回结果 True,否则返回 False。例如:
a = 20;
b = 30;
if ( a is not b ):
print "4 - a 和 b 没有相同的标识";
else:
print "4 - a 和 b 有相同的标识";
‘陆’ python两个列表字典,用list1对比list2 保留list2中不含list1的数据
请问,怎样删除list2中和list1相同的数据?,然后得到一个新的列表数据
这个是可以通过得到的
list3=[item for item in list2 if item not in list1]以title为准,主要是想筛掉list1中的内容。list3是list1剩下的,不包含list2中的数据
这个是可以通过得到
list3 = [item for item in list1 if item not in list2]
‘柒’ python 判断某个列表中的所有元素在另一个列表中
你这个标题怎么跟内容不一致。
判断一个列表中的元素是否都在另一个列表中,用集合就可以了。
>>>l1=['a','b','c']
>>>l2=['d','b','c','a']
>>>set(l1).issubset(set(l2))
True
>>>
‘捌’ python in和not in 在list中可以用吗
自然是可以的.元组列表都是可以用的.
Sequence Types — str, unicode, list, tuple, bytearray, buffer, xrange
在上面这些类型里面都是可以使用in/not in的.
‘玖’ python中 关于 list 和 string 的问题
问题1
def make_numberlist(first,last):
num=[i for i in range(first,last)]
return num
问题2
def common_element(list1,list2):
c=[]
for i in range(len(list1)):
if list1[i] in list2 and list1[i] not in c:
c.append(list1[i])
return c
问题3
def remove_section(alist,start,end):
a=alist[:]
del a[start:end+1]
return a
问题4
def add_lists(lista,listb):
c=[]
for i in range(len(lista)):
c.append(lista[i]+listb[i])
return c