如何统计python list中元素的个数及其位置?
PHP中文网
PHP中文网 2017-04-18 09:27:49
0
1
439

假设有一个list是[2,1,4,1,5,1,6,1],如果我想统计这个list中有多少个1,以及每个1的位置,应该如何写呢?谢谢~~

PHP中文网
PHP中文网

认证高级PHP讲师

reply all(1)
伊谢尔伦

count count, position index

a = [1,2,3,1]
a.count(1) # 2
a.index(1) # 0
[x for x in range(len(a)) if a[x] == 1] # 一次获得所有位置
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!