pythondict查找
Ⅰ python字典,如何查找值中包含指定字元串的鍵
1、說明python中檢測字典的鍵中是否含有某串字元,便利字典鍵值,再判斷字元串是否在鍵值中即可。2、示例代碼:# 定義一個字典dic = {'1984/1/2': 123, '1984/1/3': 0, '1985/1/1': 156}# 遍歷字典鍵中是否包含1984for key in dic: if '1984' in key: print('鍵值中包含字元串"1984"') # 或者需要的其它操作 else: print('鍵值中不包含字元串"1984"')3、執行結果:鍵值中包含字元串"1984"鍵值中不包含字元串"1984"鍵值中包含字元串"1984"
4、其它說明:python使用for in直接操作字典就是遍歷字典的鍵值,python使用in操作來判斷字元串中是否包含子串最方便,要優於使用字元串的函數index或者find。
index函數在找不到子串時會報錯,find函數會返回-1。
Ⅱ python 怎麼搜索字典里的值並且列印出來
Ⅲ python中dict的特點 dict的第一個特點是查找速度快,無論dict有10個元素還是10萬個
翻任意一本數據結構和演算法分析的書,裡面都有順序表查找和Hash表查找的例子,以及理論分析。順序表的話平均查找時間為O(n),hash表查找時間為O(1)。還有插入的時間沒有算在內。
Ⅳ python獲取字典的key值
兩種方法:for key in dict,可以一一取到key的值,或者dict.keys()可以取到key的列表。
Ⅳ python字典搜索近似的數字
Ⅵ python dict 查詢到沒有
yangyzh
Python中dict詳解
python3.0以上,print函數應為print(),不存在dict.iteritems()這個函數。
在python中寫中文注釋會報錯,這時只要在頭部加上# coding=gbk即可
#字典的添加、刪除、修改操作
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
dict["w"] = "watermelon"
del(dict["a"])
dict["g"] = "grapefruit"
print dict.pop("b")
print dict
dict.clear()
print dict
#字典的遍歷
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
for k in dict:
print "dict[%s] =" % k,dict[k]
#字典items()的使用
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
#每個元素是一個key和value組成的元組,以列表的方式輸出
print dict.items()
#調用items()實現字典的遍歷
dict = {"a" : "apple", "b" : "banana", "g" : "grape", "o" : "orange"}
for (k, v) in dict.items():
print "dict[%s] =" % k, v
#調用iteritems()實現字典的遍歷
dict = {"a" : "apple", "b" : "banana", "c" : "grape", "d" : "orange"}
print dict.iteritems()
for k, v in dict.iteritems():
print "dict[%s] =" % k, v
for (k, v) in zip(dict.iterkeys(), dict.itervalues()):
print "dict[%s] =" % k, v
Ⅶ python 值在數組或字典的查詢
#不求分只為學習交流~~
importitertools
a=[[1,2],[3,4,5],[6,8]]
b=[3,5]
#查找[3,5]in[3,4,5]
foriina:
print'bisina:',tuple(b)inlist(itertools.combinations(i,2))
#查找[3,5]in[3,4,5]以及[5,3]in[3,4,5]
b=[5,3]
foriina:
print'bisina:',tuple(b)inlist(itertools.permutations(i,2))
ps:數據量大也沒有辦法,又不能像內資料庫存儲有索引分區容等等,只有逐個遍歷
Ⅷ 如何在python列表中查找某個元素的索引
1、方法一: 利用數組自身的特性 a.index(target), 其中a是目標list,target是需要的下標對應的值。代碼如內下:
2、分片:
分片用於截取某個范圍內的元素,通過:來指定起始區間(左閉右開區間,包含左側索引值對應的元素,但不包含右測索引值對應的元素)。
分片包括起始索引對應的元素,但不包括終止索引對應的元素,索引為正值時可以發生越界但只會取到最後一個元素。如果索引值為負值,則表示從最右邊元素開始,此時需避免索引越界。
Ⅸ python如何在列表,字典中篩選數據
假設那個字典叫dict:
if dict.has_key( line[0] ):
print dict[ line[0] ]
和列表一樣,用[ ]即可
Ⅹ Python字典怎麼找到含有的項
fork,vind.items():
livers=[]
foriinv:
if'liver'ini:
livers.append(i)
iflivers:
print(k,'hastheseliversamples:')
print(' '.join(livers))