Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 一个列表中有多少前导条目在另一个列表中找到_Python_List_Count - Fatal编程技术网

Python 一个列表中有多少前导条目在另一个列表中找到

Python 一个列表中有多少前导条目在另一个列表中找到,python,list,count,Python,List,Count,我使用的是Python 3,我遇到了以下问题: ListA = [38,40,27,11,1,5,22,7,47,3,11] ListB = [12,16,38,5,40,27,3] ListA =[38,40,27,11,1,5,22,7,47,3,11] ListB = [12,16,38,5,40,27,3] count = 0 for item_a in ListA: if item_a in ListB: count += 1 else:

我使用的是Python 3,我遇到了以下问题:

ListA = [38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]
ListA =[38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

count = 0
for item_a in ListA:
    if item_a in ListB:
        count += 1
    else:
        break

print(count)  
#  3       
我需要计算ListB中的任何数字在ListA中连续出现多少次。 在本例中,输出应为3。 因为38、40和27是列表B中的数字,是列表A中的前3个数字

ListA =[38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

count = 0
for item_a in ListA:
    if item_a in ListB:
        count += 1
    else:
        break

print(count)  
#  3       
如果列表A中的第一个数字不在列表B中,则如果没有匹配项,则输出应为:0。

类似于

counter = 0
for value in A:
    if value in B:
        counter += 1
    else:
        break
ListA =[38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

count = 0
for item_a in ListA:
    if item_a in ListB:
        count += 1
    else:
        break

print(count)  
#  3       

您可以这样做:

ListA =[38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

count = 0
for item_a in ListA:
    if item_a in ListB:
        count += 1
    else:
        break

print(count)  
#  3       
只有当元素出现在ListA前面并且在ListB中找到时,它才会增加计数器,否则会中断循环。

您可以先将ListB转换为集合,以便高效地查找成员资格:

ListA =[38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

count = 0
for item_a in ListA:
    if item_a in ListB:
        count += 1
    else:
        break

print(count)  
#  3       
setB = set(ListB)
for count, a in enumerate(ListA):
    if a not in setB:
        break
else:
    count = len(ListA)

使用示例输入,count将变成:3

一种非常简洁的方法是使用函数:

ListA =[38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

count = 0
for item_a in ListA:
    if item_a in ListB:
        count += 1
    else:
        break

print(count)  
#  3       
from itertools import takewhile

ListA = [38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

sum(1 for _ in takewhile(ListB.__contains__, ListA))
# 3

takewhile将从ListA获取项目,只要它们包含在ListB 38,40,27中。输出应该是1,因为B没有30,如果是A=[12,12,30,14,12]和B=[12],输出应该是2是的,我现在知道了,我一眼就误解了这个问题。这个问题不清楚。你怎么计算3?为什么38/40/27在范围内,而不是5/3?因为数字11和1不在列表B中,我想知道列表B中的任何数字在一行中显示了多少次,您想找到一个数字在一行中出现的最大次数吗A=[12,12,15,12,12]`和B=[12]ans是3或…ans应该是2,因为12连续出现两次。
ListA =[38,40,27,11,1,5,22,7,47,3,11]
ListB = [12,16,38,5,40,27,3]

count = 0
for item_a in ListA:
    if item_a in ListB:
        count += 1
    else:
        break

print(count)  
#  3