从列表中的值中删除多余的空间 [英] Delete Extra Space from Values In List

查看:47
本文介绍了从列表中的值中删除多余的空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Python中有一个数字列表,所有数字都在前面加了一个空格.如何删除多余的空间(以便仅保留数字)?下面是一个简短的示例(请注意多余的空格):

I have a list of numbers in Python all with a extra space in the front. How do I remove the extra space (so that only the numbers are left)? A short example is below (note the extra space):

List = [' 5432', ' 23421', ' 43242', .......]

推荐答案

使用 str.lstrip ,因为空格仅位于前面:

Use str.lstrip here, as the white-space is only at the front:

List = [s.lstrip() for s in List]
# ['5432', '23421', '43242', ...]


或者在这种情况下,看到您知道有多少个空间就可以做:


Or in this case, seeing as you know how many spaces there are you can just do:

List = [s[1:] for s in List]

这篇关于从列表中的值中删除多余的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆