[英]How can it taken the specific args in lists of list?
for example we have;例如我们有;
L = [["Ak","154"],["Bm","200"],["Ck","250"], ["Ad","500"],["Ac","600"]]
I want to choose first element starting with 'A' I want to find their values which are in second element;我想选择以'A'开头的第一个元素我想找到它们在第二个元素中的值; see this output should like
看到这个 output 应该喜欢
["154","500","600"] or like [["154"],["500"],["600"]]
Filter and map with a list comprehension:使用列表理解过滤和 map:
[b for a, b in L if a[0] == "A"]
Or, if you need to search for prefixes of more than one character:或者,如果您需要搜索多个字符的前缀:
[b for a, b in L if a.startswith("A")]
Another Solution Using map()
and filter()
functions使用
map()
和filter()
函数的另一种解决方案
L = [["Ak","154"],["Bm","200"],["Ck","250"], ["Ad","500"],["Ac","600"]]
k = list(map(lambda y:y[1], list(filter(lambda x: x[0][0] == 'A' , L))))
Output: Output:
['154', '500', '600']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.