如何删除嵌套在列表中的元组?

卡西欧_3000
in_tup = [('hello', 'hi', 'bye'), ('hello', 'yes', 'no'), ('alright', 'yes', 'okay')]

目标是删除其他元组。该代码将允许用户接受前两个索引,它将删除整个元组。到目前为止,这是我的代码:

first = input()
second = input()

out_tup = [i for i in in_tup if i[0] == first]
out_tup1 = [i for i in out_tup if i[1] == second]

for i in out_tup1:
    a = ("{}, {}, {} has been removed".format(i[0],i[1],i[2]))
    print(a)

    a = [i for i in in_tup if in_tup != out_tup1]
    print(a)

但是当我输入例如“ hello”和“ hi”时,它仍然会打印所有内容:

[('hello', 'hi', 'bye'), ('hello', 'yes', 'no'), ('alright', 'yes', 'okay')]

我希望输出仅包含以下内容:

[('hello', 'yes', 'no'), ('alright', 'yes', 'okay')]
米尔塞克

在我看来,该条件in_tup != out_tup1为True,因为当输入为“ hello”和“ hi”时,您要比较两个不同长度的列表。

我相信您要尝试的是检查in_tup的元素是否在out_tup1中,因此请尝试以下操作:

a = [i for i in in_tup if i not in out_tup1]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章