简体   繁体   中英

adding string value has different beahviour im python

I am trying to calculate a date on basis of an input variable to determine an url string. For sure nothing new, but I wanted to do it on my own and I am wondering about the behavior:

Determining the variables:

year = '2020'
month = '12'
day = 12


daylist = []


for item in range(5):
    day = int(day) - 1
    if day == 0:
      day = 31
    daylist.append(day)

print(daylist)

for item in daylist:
  if item in range(0,10):
    newitem = str(0) + str(item)
    daylist.remove(item)
    daylist.append(newitem)

print(daylist)

OUtput is:
[11, 10, 9, 8, 7]
[11, 10, 8, '09', '07']

The second row should look like: [11, 10, '08', '09', '07']

Here is my question: The figure 8 does not add a zero. As you see it works well with 09 and 07 . I am a bit confused. Have any of you have an idea or can you reengineer the same problem?

not sure to understand your question here... Where is the input on your code? Also for date questions in python, you should use the datetime module instead of tweaking your own stuff (always tricky to play with dates)...

Did you mean to replace your list like this:

daylist = [item if item not in range(10) else ‘0’+str(item) for item in daylist]

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