2

好的,所以我有一长串数字,如下所示:

q = [3495790.0, 2386479.0, 2398462.0]

等等等等大约300次。我想得到这个列表的另一个版本,所有数字都加了 1800,同时保留原始列表以进行比较。谢谢你的帮助!

4

4 回答 4

5
q1 = [x + 1800 for x in q]

应该做的伎俩。

于 2011-08-02T13:55:06.067 回答
3
q2 = [x + 1800.0 for x in q]

将创建一个新列表,每个条目添加 1800。

于 2011-08-02T13:54:42.907 回答
2

在 Python 中,您可以将列表推导的结果分配给另一个列表:

# Given:
q = [3495790.0, 2386479.0, 2398462.0]

# You could do...
r = [x + 1800 for x in q]
于 2011-08-02T13:55:17.097 回答
1

Just to be accurate, this is the best way:

q2 = [x + 1800.0 for x in q]

But if you're feeling creative, you can also use a lambda with map (and by creative, I mean annoying):

q2 = map(lambda a: 1800.0 + a, q)

And, of course, there is the slow and non-pythonic

q2 = []
for i in q:
   q2.append(i)
于 2011-08-02T13:58:53.743 回答