Python根据第一项从2d数组中删除元素
16lz
2021-01-22
If I were to have a 2d array in python, say
如果我在python中有一个2d数组,请说
lst = [['a','1', '2'], ['b', 1, 2], ['c', 1, 2], ['b', 3, 4]]
I'd like a way to remove any items from lst where the first item is 'b', so that you return with:
我想要一种方法从第一项是'b'的lst中删除任何项目,以便您返回:
[['a','1', '2'], ['c', 1, 2]]
Any help would be greatly appreciated, preferred if only built in libraries are used. Thanks
如果只使用内置的库,则会非常感谢任何帮助。谢谢
2 个解决方案
#1
3
Use a list comprehension
使用列表理解
lst = [['a','1', '2'], ['b', 1, 2], ['c', 1, 2], ['b', 3, 4]]
lst = [x for x in lst if x[0] != 'b']
print(lst)
prints
[['a', '1', '2'], ['c', 1, 2]]
更多相关文章
- 无法安装ndg-httpsclient或者我的解决方案错误
- linux中awk数组应用域替换
- Linux无法连接网络解决方案
- 文本文件到字符串数组?
- 将现有数组中的所有元素传递给xargs
- Media-S 简介(一个开源的DRM解决方案)
- postgresql 数组 多了引号 空格处理
- PB怎么将动态的sql语句以及数组。传给datawindow。
- AppScan安全问题解决方案