为什么Python3的以下列表计数方法不起作用??

问题描述 投票:0回答:2

我正在尝试使用list.count()方法,但是我在输出中得到的计数为0,这是不应该发生的。

basket=[1,8,4,3,6,4,5]

count=basket.count([4, 8])

print(f'The count of {basket[6]} & {basket[2]} is',count)

[输出显示为0,应为1和2

他们是我做错了吗??

python python-3.x counting
2个回答
0
投票

来自the documentation on list

list

返回list.count(x)出现在列表中的次数。

因此x将计算整个列表basket.count([4, 8])[4, 8]中出现的次数。 basket根本不包含any列表,因此您得到零。


0
投票

查找4,8的数量:

basket
© www.soinside.com 2019 - 2024. All rights reserved.