【发布时间】:2021-05-19 15:23:10
【问题描述】:
我想确定是否可以在 python 中的 list 中的嵌套集中找到字符串列表,并打印找到它的列表或列表集。
例如:
list1 = ['cat','red']
animal = ['iguana','cat','spider','monkey','dog']
color = ['yellow','red','blue','purple','green','orange']
action = ['run','jump','swim','fly']
list2 = [animal,color,action]
list1 = set(list1)
for category in list2
if list1.issubset(type)
print(list1, "is part of", category)
结果应该是:
['cat','red'] is a part of [animal,color]
我的尝试要么失败,要么得到:
['鬣蜥','猫','蜘蛛','猴子','狗']
这是动物数组,但我只想要术语动物和颜色。
有没有办法搜索嵌套列表并打印它是哪个列表的一部分?
【问题讨论】:
-
在你的“打印”中你应该使用
type而不是list2[type]。请注意,缺少一堆:并且使用type作为变量名是很差的,因为它会使type(list1)不起作用。您的打印也不是有效的打印声明:-) -
我想我已经编辑了这个问题,以消除您可能遇到的任何困惑。如果我需要更多解释,请告诉我。
标签: python-3.x list search nested set