python列表元素個數
A. 用python怎麼統計一個列表的元素種類和各個種類的個數
統計一個列表中每一個元素的個數在Python里有兩種實現方式,
第一種是新建一個dict,鍵是列表中的元素,值是統計的個數,然後遍歷list。
items=["cc","cc","ct","ct","ac"]
count={}
foriteminitems:
count[item]=count.get(item,0)+1
print(count)
#{'ac':1,'ct':2,'cc':2}
之中用到了一個小技巧,當dict中不還沒有統計過一個元素時,直接索引count[item]會報錯,而使用get方法count.get(item, 0)能夠設置索引不存在的鍵時返回0。
第二種是使用Python內置的函數。統計元素的個數是一種非常常見的操作,Python的collection包里已經有一個Counter的類,大致實現了上面的功能。
fromcollectionsimportCounter
items=["cc","cc","ct","ct","ac"]
count=Counter(items)
print(count)
#Counter({'ct':2,'cc':2,'ac':1})
B. 如何統計python list中元素的個數及其位置
l=[1,2,3,4,1]#目標數列
targetnum=1#元素
number=l.count(targetnum)
print('個數:'+str(number))
i=number
index=[]
whilei>0:
forxinrange(len(l)):
ifl[x]==targetnum:
index.append(x+1)
i=i-1
print('位置(第幾個):'+str(index))
C. Python中怎麼快速提取List中的元素個數
commonest
=
[1,2,2,2,1,3,4,5,1,1]print(commonest.count(1))需要把數據存儲到列表中,列表的count的方法可以統計某個元素出現的次數
D. python 中如何計算列表中元素的個數
比如你可以這樣(用isinstance()函數來判斷類型):
intCount = 0 #用來記錄列表中的int元素個數
listCount = 0 #記錄list元素個數
a = [1,'a',2,[1,2]]
for i in a: #遍歷a
if isinstance(i,int): #判斷i是不是int
intCount += 1
elif isinstance(i,list): #判斷i是不是list
listCount += 1
print(intCount,listCount)結果是2 1,也就是有2個int元素,1個list元素。這是一個思路,你可以根據需要添加判斷的類型,比如要統計float類型,就可以再加個elif isinstance(i,list)來進行統計。至於元素種類,對應的記錄是0,就說明沒有這個種類的元素,比如如果intCount是0,就說明列表中沒有int元素。
E. 用Python怎麼統計一個列表的元素種類和各個種類的個數
舉個例子吧,比如一個列表numbers=["cc","cc","ct","ct","ac"]。他的元素種類有3個("cc","ct","ac"),每個種類的個數分別為2,2,1
用python統計可以編寫如下代碼;
res
=
{}
for
i
in
numbers:
res[i]
=
res.get(i,
0)
+
1
print([k
for
k
in
res.keys()])
print([v
for
v
in
res.values()])
F. Python 統計列表裡面有多少個元素
Python 統計列表裡面有多少個元素步驟如下:
1、打開python語言命令窗口,定義一個列表變數Z並列印對應的列表值。
G. 如何統計python list中元素的個數及其位置
list的元素個數即長度由len函數獲得(比如為n)。
每個元素的位置為從0開始至n-1的自然數序列。
要獲得某個元素的位置,也可以用index方法。
例如:
list1=[1,2,'3','44','555',6,7,8,[9,10]]
print(list1)
print(len(list1))
foriinrange(len(list1)):
print(list1[i])
print(list1.index('44'))
H. 求python中list的元素個數,怎麼寫
commonest
=
[1,2,2,2,1,3,4,5,1,1]
print(commonest.count(1))
需要把數據存儲到列表中,
列表的count的方法可以統計某個元素出現的次數
I. python如何用字典統計列表中不同元素個數
打開pycharm工具,新建python文件,打開文件並定義列表k,進行賦值
J. 如何統計python list中元素的個數及其位置
代碼如下:
list1=[2,3,1,9,112,23,23,6,6,1,3,0,11,2,1,1]
L1=len(list1) #列表list1的長度
list2=list(set(list1)) #可以用set,直接去掉重復的元素
list2.sort(reverse=False) #將列表由小到大排序
L2=len(list2) #列表list2的長度
print('集合:'+str(list1))
for m in range(L2):
X=set() #設定一個空的集合,用來存放這個元素的所在的位置
start=list1.index(list2[m])
for n in range(L1):
stop=L1
if list2[m] in tuple(list1)[start:stop]:
a=list1.index(list2[m],start,stop)
X.add(a)
start=start+1
print('元素:'+str(list2[m])+',一共有'+str(len(X))+'個,在列表位置集合為:'+str(X))
結果如下:
集合:[2, 3, 1, 9, 112, 23, 23, 6, 6, 1, 3, 0, 11, 2, 1, 1]
元素:0,一共有1個,在列表位置集合為:{11}
元素:1,一共有4個,在列表位置集合為:{9, 2, 14, 15}
元素:2,一共有2個,在列表位置集合為:{0, 13}
元素:3,一共有2個,在列表位置集合為:{1, 10}
元素:6,一共有2個,在列表位置集合為:{8, 7}
元素:9,一共有1個,在列表位置集合為:{3}
元素:11,一共有1個,在列表位置集合為:{12}
元素:23,一共有2個,在列表位置集合為:{5, 6}
元素:112,一共有1個,在列表位置集合為:{4}
---------------------
作者:散仙黃曉煙
來源:CSDN
原文:https://blog.csdn.net/qq_14860599/article/details/80218822
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!