I want to create a new string list from a list of strings using list comprehension. I have this list:
aa = ['AD123', 'AD223', 'AD323', 'AD423']
I want a new list of strings like this:
final = ['AD1', 'AD2', 'AD3', 'AD4']
I've found list comprehension and tried something like this:
final = map(lambda x: x[:3], aa)
Do I need a for loop to apply this on a list of strings?
您可以简单地使用您在 lambda 中创建的代码,但对于列表推导式:
new_list = [x[:3] for x in aa]
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.