Python 如何比较元组(int)的元素以确定它是否存在于列表中

Python 如何比较元组(int)的元素以确定它是否存在于列表中,python,list,for-loop,comparison,tuples,Python,List,For Loop,Comparison,Tuples,我有以下两个清单: # List of tuples representing the index of resources and their unique properties # Format of (ID,Name,Prefix) resource_types=[('0','Group','0'),('1','User','1'),('2','Filter','2'),('3','Agent','3'),('4','Asset','4'),('5','Rule','5'),('6'

我有以下两个清单:

# List of tuples representing the index of resources and their unique properties
#    Format of (ID,Name,Prefix)
resource_types=[('0','Group','0'),('1','User','1'),('2','Filter','2'),('3','Agent','3'),('4','Asset','4'),('5','Rule','5'),('6','KBase','6'),('7','Case','7'),('8','Note','8'),('9','Report','9'),('10','ArchivedReport',':'),('11','Scheduled Task',';'),('12','Profile','<'),('13','User Shared Accessible Group','='),('14','User Accessible Group','>'),('15','Database Table Schema','?'),('16','Unassigned Resources Group','@'),('17','File','A'),('18','Snapshot','B'),('19','Data Monitor','C'),('20','Viewer Configuration','D'),('21','Instrument','E'),('22','Dashboard','F'),('23','Destination','G'),('24','Active List','H'),('25','Virtual Root','I'),('26','Vulnerability','J'),('27','Search Group','K'),('28','Pattern','L'),('29','Zone','M'),('30','Asset Range','N'),('31','Asset Category','O'),('32','Partition','P'),('33','Active Channel','Q'),('34','Stage','R'),('35','Customer','S'),('36','Field','T'),('37','Field Set','U'),('38','Scanned Report','V'),('39','Location','W'),('40','Network','X'),('41','Focused Report','Y'),('42','Escalation Level','Z'),('43','Query','['),('44','Report Template ','\\'),('45','Session List',']'),('46','Trend','^'),('47','Package','_'),('48','RESERVED','`'),('49','PROJECT_TEMPLATE','a'),('50','Attachments','b'),('51','Query Viewer','c'),('52','Use Case','d'),('53','Integration Configuration','e'),('54','Integration Command f'),('55','Integration Target','g'),('56','Actor','h'),('57','Category Model','i'),('58','Permission','j')]

# This is a list of resource ID's that we do not want to reference directly, ever.
unwanted_resource_types=[0,1,3,10,11,12,13,14,15,16,18,20,21,23,25,27,28,32,35,38,41,47,48,49,50,57,58]
当这无法填充结果时,我也尝试:

result = []
for res in resource_types:
    for type in unwanted_resource_types:
        if res[0] == type:
            result.append(res[1])
也无济于事。有什么我遗漏的吗?我相信这将是执行列表理解的正确位置,但这仍然是我完全理解的灰色篮子中的内容。在这种情况下,Python文档对我来说有点过于简洁


我也愿意完全重新思考这个问题,但我确实需要保留元组列表,因为它在脚本的其他地方使用。感谢您提供的任何帮助

问题是您的三元组包含字符串,而您不需要的资源包含数字,请将数据更改为

resource_types=[(0,'Group','0'), ...
或者在比较之前使用int将字符串转换为int,这样应该可以工作。您的结果可以使用列表计算,如中所示

result=[rt[1] for rt in resource_types if int(rt[0]) in unwanted_resource_types]
如果更改“0”,则。。。变成0。。。您可以省略int调用

此外,您可以将不需要的_resource_types变量更改为一个集合,如


要提高速度,如果速度是一个问题,那么它并不重要。

问题是,您的三元组包含字符串,而不需要的资源包含数字,请将数据更改为

resource_types=[(0,'Group','0'), ...
或者在比较之前使用int将字符串转换为int,这样应该可以工作。您的结果可以使用列表计算,如中所示

result=[rt[1] for rt in resource_types if int(rt[0]) in unwanted_resource_types]
如果更改“0”,则。。。变成0。。。您可以省略int调用

此外,您可以将不需要的_resource_types变量更改为一个集合,如

如果速度是一个问题,那么提高速度就不重要了。

资源类型中的数字是字符串中包含的数字,而不需要的资源类型中的数字是普通数字,因此比较失败。这应该起作用:

result = []
for res in resource_types:
    if res[0] in unwanted_resource_types:
        result.append(res[1])
result = []
for res in resource_types:
    if int( res[0] ) in unwanted_resource_types:
        result.append(res[1])
资源类型中的数字是字符串中包含的数字,而不需要的资源类型中的数字是普通数字,因此比较失败。这应该起作用:

result = []
for res in resource_types:
    if res[0] in unwanted_resource_types:
        result.append(res[1])
result = []
for res in resource_types:
    if int( res[0] ) in unwanted_resource_types:
        result.append(res[1])

您的资源类型使用字符串,而您不需要的资源使用int,因此您需要进行一些转换以使其正常工作

试试这个:

result = []
for res in resource_types:
    if int(res[0]) in unwanted_resource_types:
        result.append(res[1])
或使用列表理解:

result = [item[1] for item in resource_types if int(item[0]) in unwanted_resource_types]

您的资源类型使用字符串,而您不需要的资源使用int,因此您需要进行一些转换以使其正常工作

试试这个:

result = []
for res in resource_types:
    if int(res[0]) in unwanted_resource_types:
        result.append(res[1])
或使用列表理解:

result = [item[1] for item in resource_types if int(item[0]) in unwanted_resource_types]
一行:

result = map(lambda x: dict(map(lambda a: (int(a[0]), a[1]), resource_types))[x], unwanted_resource_types)
没有任何显式循环就可以完成这项工作

好的-你不想在生产代码中使用它-但它很有趣-

评论: 内部dictmaplambda a:inta[0],a[1],资源类型根据输入数据创建字典:

{0: 'Group', 1: 'User', 2: 'Filter', 3: 'Agent', ...
外部映射从字典中选择名称。

一行:

result = map(lambda x: dict(map(lambda a: (int(a[0]), a[1]), resource_types))[x], unwanted_resource_types)
没有任何显式循环就可以完成这项工作

好的-你不想在生产代码中使用它-但它很有趣-

评论: 内部dictmaplambda a:inta[0],a[1],资源类型根据输入数据创建字典:

{0: 'Group', 1: 'User', 2: 'Filter', 3: 'Agent', ...

外部地图从字典中选择名称。

谢谢!这会很有效的。与其在运行时转换res[0],我将把每个元组中的第一个元素转换为int,这样代码看起来会更好,但我仍然希望通过您编写的列表理解。@EthanFurman这两个代码片段给出相反的结果,显然其中一个是错的。多么尴尬-修复了第一个。我以为霍奇尔指的是第二个。@EthanFurman我想霍奇尔指的是第二个。如果你看这个问题,预期的结果是[‘组’、‘用户’、‘代理’、…]。使用not in可提供['Filter'、'Asset'、'Rule'、'KBase'…]谢谢!这会很有效的。与其在运行时转换res[0],我将把每个元组中的第一个元素转换为int,这样代码看起来会更好,但我仍然希望通过您编写的列表理解。@EthanFurman这两个代码片段给出相反的结果,显然其中一个是错的。多么尴尬-修复了第一个。我以为霍奇尔指的是第二个。@EthanFurman我想霍奇尔指的是第二个。如果你看这个问题,预期的结果是[‘组’、‘用户’、‘代理’、…]。在[Filter]、[Asset]、[Rule]、[KBase'…]中使用not会让人感觉很棒。出于代码健全的原因,我打算将该元组中的第一个元素转换为int。即使这并不重要,因为我怀疑执行时间会改变,我会将整数列表转换为一个集合,速度越快越好,对吗;我也在努力将最佳实践灌输到我的头脑中。谢谢伟大的出于代码健全的原因,我打算将该元组中的第一个元素转换为int。即使这并不重要,因为我怀疑执行时间会改变,我会将整数列表转换为一个集合,速度越快越好,对吗;我也在努力将最佳实践灌输到我的头脑中。谢谢我喜欢这个。不打算使用它,但我喜欢它:没有人会调试它:-我喜欢它。不去
使用它,但我喜欢它:没有人会调试它:-