![](/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.