简体   繁体   English

如何检查所有元素是否属于一组?

[英]How do I check if all elements fall in one group?

I have different product groups, let's say toys, clothes and food.我有不同的产品组,比如说玩具、衣服和食物。 When I get a series (or list) of products I want to know if they all fall in one group or if I have products from a few groups.当我得到一系列(或列表)产品时,我想知道它们是否都属于一个组,或者我是否有几个组的产品。

Let's make an example:让我们举个例子:

toys=['Car', 'Teddy Bear', 'Doll']
food=['Banana', 'Cola', 'Bread', 'Milk']

So when I get ['Cola', 'Milk'] I want it to return True (they all fall in food) and when I get ['Milk', 'Banana', 'Car'] I want it to return False (contains products of both toys AND food ).所以当我得到['Cola', 'Milk']我希望它返回 True (它们都掉进食物中)而当我得到['Milk', 'Banana', 'Car']我希望它返回 False (包含toysfood的产品)。

Is there a way to check this other than looping trough all my groups and checking one by one if all products are in that specific group?除了循环遍历我的所有组并逐个检查所有产品是否都在该特定组中之外,还有其他方法可以检查吗?

Thank you!谢谢!

This is essentially a set operation.这本质上是一个集合操作。 You can convert the toys and food to sets and then use set.intersection .您可以将toysfood转换为套装,然后使用set.intersection For example:例如:

toys = ["Car", "Teddy Bear", "Doll"]
food = ["Banana", "Cola", "Bread", "Milk"]

toys, food = set(toys), set(food)


def is_only_one_group(l):
    return bool(toys.intersection(l)) + bool(food.intersection(l)) == 1


print(is_only_one_group(["Milk", "Banana", "Car"]))
print(is_only_one_group(["Cola", "Milk"]))

Prints:印刷:

False
True

You can build dictionaries out of your lists such that entries of the lists are the keys and values are the name of the list they belong to.您可以从列表中构建字典,以便列表条目是键,值是它们所属列表的名称。 Then we can map the items in the new list and check if the number of unique elements is 1 or not:然后我们可以 map 新列表中的项目并检查唯一元素的数量是否为 1:

toys_dict = dict.fromkeys(toys, "toys")
food_dict = dict.fromkeys(food, "food")

combined_dict = {**toys_dict, **food_dict}

def is_only_one_group(new_list):
    return len(set(combined_dict[val] for val in new_list)) == 1

using as用作

>>> is_only_one_group(["Cola", "Milk"])
True

>>> is_only_one_group(["Milk", "Banana", "Car"])
False

You could use the set data structure in Python.您可以使用 Python 中的设置数据结构。

  • For each group, check if your input list is a subset.对于每个组,检查您的输入列表是否是一个子集。
  • Then verify that it is subset of exactly one group.然后验证它是否恰好是一组的子集。
toys=['Car', 'Teddy Bear', 'Doll']
food=['Banana', 'Cola', 'Bread', 'Milk']

def is_in_one_group(my_list):
    belongs_in_group = [set(my_list).issubset(toys), set(my_list).issubset(food)]
    return sum(belongs_in_group) == 1


is_in_one_group(['Cola', 'Milk'])
# True

is_in_one_group(['Milk', 'Banana', 'Car'])
# False

is_in_one_group([])
# False

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查列表中的所有元素是否相同? - How do I check if all elements in a list are the same? 如何检查嵌套列表树的所有元素是否相同? - How do I check if all the elements of a nested list tree are identical? 如何快速检查numpy数组的所有元素是否为浮点数? - How do I quickly check if all elements of numpy array are floats? 如何检查一个列表是否包含另一个列表的 2 个元素? - How do I check if one list contains 2 elements of another list? 如何针对 list_b 中的每个元素检查 list_a 的一个索引,并对 list_a 中的所有元素重复此过程? - How do I check one index of list_a against every element in list_b, and repeat this process for all elements in list_a? 如何将Python中的所有函数归为一个函数? - How do I group all my functions in Python into one function? 如何列出属于一组范围的所有数字对? - How to list all the pairs of numbers which fall under a group of range? 如何通过查看一个数据框中的日期落在另一个数据框中的日期范围内来合并Pandas数据框? - How do I combine Pandas dataframes by looking at dates in one dataframe that fall within a date range in another dataframe? 当一个列表的所有元素都在另一个列表中时,如何分组和求和 - How to group by and sum when all elements of one list are in another list 如何检查列表中的所有元素是否都包含在其他元素中 - How to check if all elements of a list are contained in other one
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM