简体   繁体   English

列表列表:如何删除所有包含特定值的列表?

[英]List of lists: how to delete all lists that contain certain values?

There's a list of lists (or it could be tuple of tuples).有一个列表列表(或者它可能是元组的元组)。 For example:例如:

my_list = [
  ['A', 7462],
  ['B', 8361],
  ['C', 3713],
]

What would be the most efficient way to filter out all lists that have a value 'B' in them, considering that the number (or other values) might change?考虑到数字(或其他值)可能会发生变化,过滤掉其中包含值'B'的所有列表的最有效方法是什么?

The only way I came up with so far is using a for/in cycle (or rather a list comprehension) but it's very inefficient in this case, so I'd like to know if it's possible to avoid using a loop.到目前为止我想出的唯一方法是使用 for/in 循环(或者更确切地说是列表理解),但在这种情况下效率非常低,所以我想知道是否可以避免使用循环。

You can create a new list, filtering out the stuff you don't want.您可以创建一个新列表,过滤掉您不想要的内容。 In python this is usually done with a list comprehension:在 python 中,这通常是通过列表理解来完成的:

new_list = [sublist for sublist in my_list if sublist[0] != 'B']

You can use also use double-list comprehension.您也可以使用双列表理解。 [ [] ]

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

相关问题 如何在列表列表中找到具有最大值的列表(嵌套列表包含字符串和数字)? - How to find the lists with max values in a list of lists (where nested lists contain strings and numbers)? 如何访问包含列表作为值的字典中列表值中的最后一个元素 - How to access the last element in the list values in a dictionary that contain lists as values 如何从两个列表中的一个列表中有效删除具有特定值的所有索引? - How to efficiently delete all indices with certain value in one list out of both lists? 在列表python中查找包含0-23中所有值的列表 - Find lists which together contain all values from 0-23 in list of lists python 删除列表列表中包含特定 position 中特定项目的列表 - Delete lists in list of lists that contain specific item in specific position 如何在列表列表中查找某些列表的索引? - How to find the index of certain lists in a list of lists? 从列表python中删除所有出现的特定值 - Delete all occurrences of specific values from list of lists python 如何删除列表字典的值 - How to delete values of a dictionary of lists 如何创建满足特定条件的所有可能列表的列表? - How to create a list of all possible lists satisfying a certain condition? 如何从python中的列表列表中提取某些值? - How do I pull certain values from a list of lists in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM