新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:如何判斷列表中不包含某個(gè)元素
首先先定義一個(gè)列表,并在其中添加一些元素,然后來判斷某一元素是否在其中。

例如
In [7]: test_list = [1,5,7,4,23,66] In [8]: a = 25 In [9]: a not in test_list
以上實(shí)例輸出結(jié)果為:
True
例如
from bisect import bisect_left
# 初始化列表
test_list_set = [ 1, 6, 3, 5, 3, 4 ]
test_list_bisect = [ 1, 6, 3, 5, 3, 4 ]
print("查看 4 是否在列表中 ( 使用 set() + in) : ")
test_list_set = set(test_list_set)
if 4 in test_list_set :
print ("存在")
print("查看 4 是否在列表中 ( 使用 sort() + bisect_left() ) : ")
test_list_bisect.sort()
if bisect_left(test_list_bisect, 4):
print ("存在")輸出結(jié)果為
查看 4 是否在列表中 ( 使用 set() + in) : 存在 查看 4 是否在列表中 ( 使用 sort() + bisect_left() ) : 存在
更多學(xué)習(xí)內(nèi)容,請點(diǎn)擊Python學(xué)習(xí)網(wǎng)。
文章標(biāo)題:創(chuàng)新互聯(lián)Python教程:如何判斷列表中不包含某個(gè)元素
URL網(wǎng)址:http://m.fisionsoft.com.cn/article/dhcsghh.html


咨詢
建站咨詢
