好的,所以我有一长串数字,如下所示:
q = [3495790.0, 2386479.0, 2398462.0]
等等等等大约300次。我想得到这个列表的另一个版本,所有数字都加了 1800,同时保留原始列表以进行比较。谢谢你的帮助!
q1 = [x + 1800 for x in q]
应该做的伎俩。
q2 = [x + 1800.0 for x in q]
将创建一个新列表,每个条目添加 1800。
在 Python 中,您可以将列表推导的结果分配给另一个列表:
# Given:
q = [3495790.0, 2386479.0, 2398462.0]
# You could do...
r = [x + 1800 for x in q]
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)