简体   繁体   中英

(python) How do I modify a list?

I have a list that looks something like this: days_list = ['12','554','43','2343','52']

How do I modify it so that it looks like this: days_list = ['days:12', 'days:554', 'days:43', 'days:2343', 'days:52'] ?

根据旧列表创建一个新列表,其中每个元素都是一个带有前缀“days:”的字符串,然后是原始值。

days_list = ['days:{}'.format(value) for value in days_list]

使用列表理解,你可以很容易地做到这一点

result = ['days:'+item for item in days_list]

Write the code as:

days_list = ['days:{}'.format(value) for value in days_list]

or

days_list = [f'days:{value}' for value in days_list]

or

days_list = [f'days:{days_list[x]}' for x in range(0, len(days_list))

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