列表結合index和count方法的使用

2021-10-19 13:19:43 字數 535 閱讀 9528

index: 根據指定資料獲取該資料在列表中的下標

count: 根據指定資料在列表中統計該資料出現的次數

my_list = ["a", 'c', "b", 2, 1, 'a']

result = my_list.index("b")

print(result)

#2result = my_list.index(2)

print(result)

#3# 1. 要查詢的資料

# 2. 開始下標

# 3. 結束下標(不包含)

result = my_list.index("c", 0, 2)

print(result)

#1result = my_list.count("a")

print("a出現了%d次" % result)

#a出現了2次

result = my_list.count(5)

print("5出現了%d次" % result)

#5出現了0次

元組中常用方法index 和count

一 元組中index 方法 1 index 作用 python 元組 index 方法用於從元組中找出某個物件第乙個匹配項的索引位置,如果這個物件不在元組中會報乙個異常。2 index 方法語法 t.index obj start 0 end len t 3 引數 obj 指定檢索的物件。start...

Sass函式 值列表index

ndex 函式類似於索引一樣,主要讓你找到某個值在列表中所處的位置。在 sass 中,第乙個值就是1,第二個值就是 2,依此類推 index 1px solid red,1px 1 index 1px solid red,solid 2 index 1px solid red,red 3 在 ind...

count 1 和count 哪個高效?

當表的資料量大些時,對錶作分析之後,使用count 1 還要比使用count 用時多了!從執行計畫來看,count 1 和count 的效果是一樣的。但是在表做過分析之後,count 1 會比count 的用時少些 1w以內資料量 不過差不了多少。如果count 1 是聚索引,id,那肯定是coun...