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

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

PHP中文网
PHP中文网

认证高级PHP讲师

répondre à tous(1)
伊谢尔伦

compte, indice de position

a = [1,2,3,1]
a.count(1) # 2
a.index(1) # 0
[x for x in range(len(a)) if a[x] == 1] # 一次获得所有位置
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!