![](/img/trans.png)
[英]How to feed a list of lists into *args when the number of inside lists is unknown?
[英]How can it taken the specific args in lists of list?
例如我们有;
L = [["Ak","154"],["Bm","200"],["Ck","250"], ["Ad","500"],["Ac","600"]]
我想选择以'A'开头的第一个元素我想找到它们在第二个元素中的值; 看到这个 output 应该喜欢
["154","500","600"] or like [["154"],["500"],["600"]]
使用列表理解过滤和 map:
[b for a, b in L if a[0] == "A"]
或者,如果您需要搜索多个字符的前缀:
[b for a, b in L if a.startswith("A")]
使用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:
['154', '500', '600']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.