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

我正在尝试使用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

他们是我做错了什么吗?

评论
jsed
jsed

From the documentation on list:

list.count(x)

Return the number of times x appears in the list.

So basket.count([4, 8]) will count how many times the whole list [4, 8] appears in basket. basket doesn't contain any lists at all, so you get zero.

点赞
评论