How to delete the very last character from every string in a list of strings

后端 未结 4 641
长发绾君心
长发绾君心 2021-01-11 12:55

I have the strings \'80010\', \'80030\', \'80050\' in a list, as in

test = [\'80010\',\'80030\',\'80050\']

How can I delete the very last

4条回答
  •  南笙
    南笙 (楼主)
    2021-01-11 13:23

    In python @Matthew solution is perfect. But if indeed you are a beginer in coding in general, I must recommend this, less elegant for sure but the only way in many other scenario :

    #variables declaration
    test = ['80010','80030','80050'] 
    lenght = len(test)                 # for reading and writing sakes, len(A): lenght of A
    newtest = [None] * lenght          # newtest = [none, none, none], go look up empty array creation
    strLen = 0                         # temporary storage
    
    #adding in newtest every element of test but spliced
    for i in range(0, lenght):         # for loop
        str = test[i]                  # get n th element of test
        strLen = len (str)             # for reading sake, the lenght of string that will be spliced
        newtest[i] = str[0:strLen - 1] # n th element of newtest is the spliced n th element from test
    
    #show the results
    print (newtest)                    # ['8001','8003','8005']
    

    ps : this scripts, albeit not being the best, works in python ! Good luck to any programmer newcommer.

提交回复
热议问题