如何不使用'[]'和''将字符串转换为列表

时间:2018-10-06 22:20:47

标签: python python-3.x python-2.7

Revenue = [400000000,10000000,10000000000,10000000]
s1 = []

for x in Revenue:
    message = (','.join(['{:,.0f}'.format(x)]).split())
    s1.append(message)
print(s1)

The output I am getting is something like this [['400,000,000'], ['10,000,000'], ['10,000,000,000'], ['10,000,000']] and I  want it should be like this -> [400,000,000, 10,000,000, 10,000,000,000, 10,000,000]

有人可以帮我吗,我是python新手

2 个答案:

答案 0 :(得分:1)

如果您的目标只是添加逗号,则由于' '会成为str,因此您会被Revenue = [400000000,10000000,10000000000,10000000] l = ['{:,}'.format(i) for i in Revenue] # ['400,000,000', '10,000,000', '10,000,000,000', '10,000,000'] 所困扰,但是您可以使用更简单的列表理解

quotes

您还可以将列表解压缩为变量,然后在不使用v, w, x, y = l print(v) # 400,000,000 的情况下打印每个变量

print

您可以print(*l) # 400,000,000 10,000,000 10,000,000,000 10,000,000 拆箱清单,但这只会输出

l = []
for i in Revenue:
    l.append('{:,}'.format(i))

扩展循环:

var xScale = d3.scalePoint()
  .domain(dataset.map(d => d.name)) // input is an array of names
  .range([0, width]); // output

答案 1 :(得分:0)

我不确定您为什么想要显示的输出,因为它很难阅读,但是这里是制作方法:

>>> Revenue = [400000000,10000000,10000000000,10000000]
>>> def revenue_formatted(rev):
...     return "[" + ", ".join("{:,d}".format(n) for n in rev) + "]"
...
>>> print(revenue_formatted(Revenue))
[400,000,000, 10,000,000, 10,000,000,000, 10,000,000]