Python – 给定列表中子列表的计数频率
给定一个列表和一个子列表,计算列表中子列表的出现次数。
Input : test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 3, 5, 7], sublist = [3, 5, 7]
Output : 3
Explanation : 3, 5, 7 occurs 3 times.
Input : test_list = [4, 5, 3, 5, 8, 8, 3, 2, 7, 2, 3, 6, 7], sublist = [3, 5, 7]
Output : 0
Explanation : No occurrence found.
方法#1:使用列表理解+切片
在此,我们测试使用切片提取的列表的每个子列表,如果找到,则将元素添加到列表中,最后使用 len() 计算列表的长度。
Python3
# Python3 code to demonstrate working of
# Sublist Frequency
# Using list comprehension + slicing
# initializing list
test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
# printing original list
print("The original list is : " + str(test_list))
# initializing Sublist
sublist = [3, 5, 7]
# slicing is used to extract chunks and compare
res = len([sublist for idx in range(len(test_list)) if test_list[idx : idx + len(sublist)] == sublist])
# printing result
print("The sublist count : " + str(res))
Python3
# Python3 code to demonstrate working of
# Sublist Frequency
# Using zip_longest() + islice() + all() + loop
from itertools import zip_longest, islice
# initializing list
test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
# printing original list
print("The original list is : " + str(test_list))
# initializing Sublist
sublist = [3, 5, 7]
# slicing is used to extract chunks and compare
res = []
idx = 0
while True:
try:
# getting to the index
idx = test_list.index(sublist[0], idx)
except ValueError:
break
# using all() to check for all elements equivalence
if all(x == y for (x, y) in zip_longest(sublist, islice(test_list, idx, idx + len(sublist)))):
res.append(sublist)
idx += len(sublist)
idx += 1
res = len(res)
# printing result
print("The sublist count : " + str(res))
输出
The original list is : [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
The sublist count : 2
方法#2:使用 zip_longest() + islice() + all() + loop
在此,我们使用 islice() 执行切片任务并使用 all() 检查每个元素是否匹配, zip_longest() 有助于映射元素以检查子列表中的相等性。循环用于获取与列表中子列表的第一个元素的索引匹配,以提高效率。
Python3
# Python3 code to demonstrate working of
# Sublist Frequency
# Using zip_longest() + islice() + all() + loop
from itertools import zip_longest, islice
# initializing list
test_list = [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
# printing original list
print("The original list is : " + str(test_list))
# initializing Sublist
sublist = [3, 5, 7]
# slicing is used to extract chunks and compare
res = []
idx = 0
while True:
try:
# getting to the index
idx = test_list.index(sublist[0], idx)
except ValueError:
break
# using all() to check for all elements equivalence
if all(x == y for (x, y) in zip_longest(sublist, islice(test_list, idx, idx + len(sublist)))):
res.append(sublist)
idx += len(sublist)
idx += 1
res = len(res)
# printing result
print("The sublist count : " + str(res))
输出
The original list is : [4, 5, 3, 5, 7, 8, 3, 5, 7, 2, 7, 3, 2]
The sublist count : 2