Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从字符串列表中的每个字符串中删除最后一个字符_Python_String_List - Fatal编程技术网

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

Python 如何从字符串列表中的每个字符串中删除最后一个字符,python,string,list,Python,String,List,我在列表中有字符串“80010”、“80030”、“80050”,如 test = ['80010','80030','80050'] 如何删除最后一个字符(在本例中是每个字符串的最后一个数字,即0),以便最终得到另一个列表,其中仅包含每个字符串的前四个数字/字符?所以最后会有这样的结果 newtest = ['8001', '8003', '8005'] 我对Python非常陌生,但我尝试过使用if-else语句、追加、使用索引[:-1]等,但除非最终删除所有其他零,否则一切似乎都不起作用

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

test = ['80010','80030','80050']
如何删除最后一个字符(在本例中是每个字符串的最后一个数字,即0),以便最终得到另一个列表,其中仅包含每个字符串的前四个数字/字符?所以最后会有这样的结果

newtest = ['8001', '8003', '8005']
我对Python非常陌生,但我尝试过使用if-else语句、追加、使用索引[:-1]等,但除非最终删除所有其他零,否则一切似乎都不起作用。非常感谢你

test = ["80010","80030","80050"]
newtest = [x[:-1] for x in test]
新测试将包含结果
[“8001”、“8003”、“8005”]


[x[:-1]用于测试中的x]
通过在
test
中循环每个项目并将修改后的版本放入
newtest
中,创建一个新列表(使用列表理解)。
x[:-1]
意味着将字符串值x中的所有内容都取到最后一个元素,但不包括最后一个元素。

。使用切片表示法[:-1]是正确的方法。只需将其与列表结合即可:

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

somestring[:-1]
提供了从位置0处的字符(包括)到最后一个字符(排除)的所有内容。

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

具有映射功能

test = ['80010','80030','80050']
print map(lambda x: x[:-1],test)
# ['8001', '8003', '8005']
有关此解决方案的更多信息,请阅读我在另一个类似问题中所做的简要说明


在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:这个脚本虽然不是最好的,但可以在python中工作!祝新来的程序员好运。

我也遇到过类似的问题,下面是解决方案

List<String> timeInDays = new ArrayList<>();
timeInDays.add(2d);
timeInDays.add(3d);
timeInDays.add(4d);
List timeInDays=new ArrayList();
添加(2d);
添加(3d);
时间天数。添加(4d);
我需要删除每个字符串中的最后一个字母,以便比较它们。下面的解决方案对我有效

List<String> trimmedList = new ArrayList<>;
for(int i=0;i<timeInDays.size();i++)
{
String trimmedString = timeInDays.get(i).substring(0,name.length()-1);
trimmedList=add(trimmedString );
}

System.out.println("List after trimming every string is "+trimmedList);
List trimmedList=newarraylist;

对于(int i=0;给我自己一个很好的解释XDI使用了您的解决方案,但它没有从列表中的每个元素中删除最后一个字符,而是删除了最后一个元素,我不明白这在Python2中的作用。如果您使用Python3,map函数将返回一个生成器(根据请求每次返回1项),因此,如果需要列表(
list(map(lambda x:x[:-1],test))
),则需要将其包装在列表调用中。此外,print表达式在python 3中成为一个函数,因此必须将要打印的内容也包装在括号中。感谢您的添加。