在列表列表中找到特定列表的次数

How many times specific list is found in a list of lists

我有以下列表

a = ['Bananas', 'Ananas', 'Peach', 'Grapes', 'Oranges']

以及以下列表列表

b = [['Bananas', 'Ananas', 'Peach', 'Grapes', 'Oranges'], ['Bananas', 'Ananas', 'Peach', 'Grapes', 'Oranges', 'Pear', 'Apple'], ['Oranges', 'Strawberry', 'Pear'], ... ]

如您所见,在 b 内部可能有

  1. a
  2. 一模一样的列表
  3. 包含与 a 相同项目但顺序不同的列表
  4. 与 1 和 2 相似,但比 a
  5. 的项目更多
  6. 与 1 和 2 类似,但项目少于 a
  7. a
  8. 完全不同的列表

考虑使用

for value in b:
    print(value)

一个人能够得到 b 的每个列表,然后可以将其与 a 进行比较,如何知道多少次情况 1 , 2 和 3 出现(包括重复)?


受到 this answer 的启发,我进行了实验

count_matches = 0

for value in b:
    ff = str(value).strip("[]")
    gg = str(a).strip("[]")
    if gg in ff:
        count_matches += 1

print(count_matches)

但是由于订单的原因,这没有用(例如,其他项目可以在中间添加)。

为了解决这个问题我结合了this approach和for循环来获取值,如

count_matches = 0
for value in b:
    if set(a) <= set(value):
        count_matches += 1
        print("a is in b")