简体   繁体   中英

How can I replace values of a list?

I have a list like this one:

['node.volume=Mon Jun 15 16:52:18 2020','node.volume2=Mon Jun 16 17:11:18 2020']

And I have the days elapsed from the date specified on my first list, and the current day, returning a list like this one.

[8, 8, 8]

How can I replace the format of the first list, with the days of the second?

To have a list like this one:

['node.volume=8','node.volume2=8']

You can use script like this:

nodevolumes=["node.volume=Mon Jun 15 16:52:18 2020","node.volume2=Mon Jun 16 17:11:18 2020", "node.volume2=Mon Jun 16 17:11:18 2020"]
dayselapsed=[8, 8, 8]
new_nodevolumes=[]
for i in range(len(nodevolumes)):
    vol=nodevolumes[i].split("=")
    new_nodevolumes.append(vol[0]+"="+str(dayselapsed[i]))
print(new_nodevolumes) 

Or without using days elapsed as:

import datetime as datetime
nodevolumes=["node.volume=Mon Jun 15 16:52:18 2020","node.volume2=Mon Jun 16 17:11:18 2020", "node.volume2=Mon Jun 16 17:11:18 2020"]
#dayselapsed=[8, 8, 8]
new_nodevolumes=[]
for i in range(len(nodevolumes)):
    vol=nodevolumes[i].split("=")
    #new_nodevolumes.append(vol[0]+"="+str(dayselapsed[i]))
    elapsed=datetime.datetime.now()-datetime.datetime.strptime(vol[1], '%a %b %d %H:%M:%S %Y')
    new_nodevolumes.append(vol[0]+"="+str(elapsed.days))
print(new_nodevolumes)

Hope this helps.

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