从 Python 中的列表中删除所有逗号
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15748095/
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
Removing all commas from list in Python
提问by Jose Rodriguez
My issue: I'd like to add all the digits in this string '1.14,2.14,3.14,4.14'but the commas are causing my sum function to not work correctly.
I figured using a strip function would solve my issue but it seems as though there is still something I'm missing or not quite understanding.
我的问题:我想添加此字符串中的所有数字,'1.14,2.14,3.14,4.14'但逗号导致我的 sum 函数无法正常工作。
我认为使用 strip 函数可以解决我的问题,但似乎仍有一些我遗漏或不太理解的东西。
total = 0
for c in '1.14,2.14,3.14'.strip(","):
total = total + float(c)
print total
I have searched how to remove commas from a string but I only found information on how to remove commas from the beginning or ending of a string.
我搜索了如何从字符串中删除逗号,但我只找到了有关如何从字符串的开头或结尾删除逗号的信息。
Additional Info: Python 2.7
附加信息:Python 2.7
采纳答案by Dan Lecocq
I would use the following:
我会使用以下内容:
# Get an array of numbers
numbers = map(float, '1,2,3,4'.split(','))
# Now get the sum
total = sum(numbers)
回答by alestanis
You don't want strip, you want split.
你不想要strip,你想要split。
The splitfunction will separate your string into an array, using the separator character you pass to it, in your case split(',').
该split函数将使用您传递给它的分隔符将您的字符串分成一个数组,在您的情况下split(',')。
回答by Rob?
This will add all of the digits in the first string in your question:
这将添加问题中第一个字符串中的所有数字:
sum(float(x) for x in '1.14,2.14,3.14,4.14' if x.isdigit())
回答by Nadir Sampaoli
Since there seem to be a pattern in your input list of floats, this one-liner generates it:
由于您的输入浮点数列表中似乎存在一种模式,因此此单行代码会生成它:
>>> sum(map(float, ','.join(map(lambda x:str(x+0.14), range(1,5))).split(',')))
10.559999999999999
And since it doesn't make much sense joining with commas and immediately splitting by commas, here's a little saner piece of code:
因为用逗号连接并立即用逗号分隔没有多大意义,所以这里有一段更理智的代码:
>>> sum(map(float, map(lambda x:str(x+0.14), range(1,5))))
10.559999999999999
And if you actually meant you wanted to sum single digits and not the actual floating-point numbers (although I doubt it since you cast to float in your sample code):
如果您实际上是想对单个数字求和而不是对实际的浮点数求和(尽管我对此表示怀疑,因为您在示例代码中强制转换为浮点数):
>>> sum(map(int, ''.join(map(lambda x:str(x+0.14), range(1,5))).replace('.', '')))
30
回答by pradyunsg
You need splitnot strip.
你需要split不strip。
>>> for c in '1,2,3,4,5,6,7,8,9'.split(","):
print float(c)
1.0
2.0
3.0
4.0
5.0
6.0
7.0
8.0
9.0
Or if you want a list comprehension:
或者,如果您想要列表理解:
>>> [float(c) for c in '1,2,3,4,5,6,7,8,9'.split(",")]
[1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0]
And for getting the sum,
为了得到总和,
>>> sum(map(float, '1,2,3,4,5,6,7,8,9'.split(",")))
45.0
回答by Fasih Khan
values=input()
l=values.split(",")
print(l)
With values=1,2,3,4,5
值=1,2,3,4,5
the result is ['1','2','3','4','5']
结果是 ['1','2','3','4','5']
回答by Shreyas Dangare
Use this to remove commas and white spaces
使用它来删除逗号和空格
a = [1,2,3,4,5]
print(*a, sep = "")
Output:- 12345
输出:- 12345

