Python 如何从字符串列表中的每个字符串中删除最后一个字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34866781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 15:39:11  来源:igfitidea点击:

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

pythonstringlist

提问by spartmar

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

我在列表中有字符串 '80010'、'80030'、'80050',如

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

How can I delete the very last character (in this case the very last digit of each string which is a 0), so that I can end up with another list containing only the first four digits/characters from each string? So end up with something like

如何删除最后一个字符(在这种情况下,每个字符串的最后一个数字为 0),以便我可以得到另一个仅包含每个字符串前四位数字/字符的列表?所以最终得到类似的东西

newtest = ['8001', '8003', '8005']

I am very new to Python but I have tried with if-else statements, appending, using indexing [:-1], etc. but nothing seems to work unless I end up deleting all my other zeros. Thank you so much!

我对 Python 非常陌生,但我尝试过使用 if-else 语句、附加、使用索引 [:-1] 等,但除非我最终删除所有其他零,否则似乎没有任何效果。非常感谢!

采纳答案by Matthew

test = ["80010","80030","80050"]
newtest = [x[:-1] for x in test]

New test will contain the result ["8001","8003","8005"].

新测试将包含结果["8001","8003","8005"]

[x[:-1] for x in test]creates a new list (using list comprehension) by looping over each item in testand putting a modified version into newtest. The x[:-1]means to take everything in the string value x up to but not including the last element.

[x[:-1] for x in test]创建一个新的列表(使用列表中理解通过遍历每个项目)test,并把修改后的版本为newtest。将x[:-1]字符串值 x 中的所有内容取到但不包括最后一个元素的方法。

回答by timgeb

You are not so far off. Using the slice notation [:-1] is the right approach. Just combine it with a list comprehension:

你并没有那么远。使用切片符号 [:-1] 是正确的方法。只需将它与列表理解结合起来:

>>> test = ['80010','80030','80050']
>>> [x[:-1] for x in test]
['8001', '8003', '8005']

somestring[:-1]gives you everything from the character at position 0 (inclusive) to the last character (exclusive).

somestring[:-1]为您提供从位置 0(包含)的字符到最后一个字符(不包含)的所有内容。

回答by Rockybilly

Just to show a slightly different solution than comprehension, Given that other answers already explained slicing, I just go through at the method.

只是为了展示一个与理解略有不同的解决方案,鉴于其他答案已经解释了切片,我只介绍了该方法。

With the map function.

带地图功能。

test = ['80010','80030','80050']
print map(lambda x: x[:-1],test)
# ['8001', '8003', '8005']

For more information about this solution, please read the brief explanation I did in another similar question.

有关此解决方案的更多信息,请阅读我在另一个类似问题中所做的简要说明。

Convert a list into a sequence of string triples

将列表转换为字符串三元组序列

回答by NRagot

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 :

在python中@Matthew解决方案是完美的。但是,如果您确实是一般编码的初学者,我必须推荐这个,肯定不太优雅,但这是许多其他情况下的唯一方法:

#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.

ps:这个脚本虽然不是最好的,但可以在 python 中运行!祝任何程序员新人好运。