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