![](/img/trans.png)
[英]How to find the lists with max values in a list of lists (where nested lists contain strings and numbers)?
[英]List of lists: how to delete all lists that contain certain values?
有一個列表列表(或者它可能是元組的元組)。 例如:
my_list = [
['A', 7462],
['B', 8361],
['C', 3713],
]
考慮到數字(或其他值)可能會發生變化,過濾掉其中包含值'B'
的所有列表的最有效方法是什么?
到目前為止我想出的唯一方法是使用 for/in 循環(或者更確切地說是列表理解),但在這種情況下效率非常低,所以我想知道是否可以避免使用循環。
您可以創建一個新列表,過濾掉您不想要的內容。 在 python 中,這通常是通過列表理解來完成的:
new_list = [sublist for sublist in my_list if sublist[0] != 'B']
您也可以使用雙列表理解。 [ [] ]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.