开发者

How to keep a list static in Python?

Alright, so I have a long list of numbers, like so:

q = [3开发者_C百科495790.0, 2386479.0, 2398462.0]

and so on and so on about 300 times. I want to get another version of this list with all the numbers added by 1800, while retaining the original list for comparison purposes. Thank you for all your help!


q1 = [x + 1800 for x in q]

should do the trick.


q2 = [x + 1800.0 for x in q]

will create a new list with 1800 added to each entry.


In Python, you can just assign the result of a list comprehension to another list:

# 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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜