简体   繁体   中英

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; see this output should like

["154","500","600"] or like [["154"],["500"],["600"]]

Filter and map with a list comprehension:

[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

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']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM