比较来自世界各地的卖家的域名和 IT 服务价格

如何将整数添加到每个列表项?

如果我有
list=[1,2,3]

我想补充一下
1

每个元素都可以出路
[2,3,4]

,
我该怎么做?

我假设我会使用 for loop, 但我不确定如何做到这一点。
已邀请:

小姐请别说爱

赞同来自:

new_list = [x+1 for x in my_list]

小姐请别说爱

赞同来自:

>>> mylist = [1,2,3]
>>> [x+1 for x in mylist]
[2, 3, 4]
>>>


http://docs.python.org/tutoria ... sions
.

莫问

赞同来自:

理解列表的其他答案可能最适合添加添加,但如果您有更复杂的函数,则需要应用于所有元素,然后
http://docs.python.org/library/functions.html#map
可能是合适的。

在你的榜样中,这将是:


>>> map/lambda x:x+1, [1,2,3]/
[2,3,4]

裸奔

赞同来自:

如果你想使用 numpy, 还有另一种方法如下。


import numpy as np
list1 = [1,2,3]
list1 = list/np.asarray/list1/ + 1/

帅驴

赞同来自:

编辑:它不是现场

首先,不要使用这个词 'list' 对于你的变量。 他曲调了一个关键字
list

.

最好的方法是通过拼接来做到这一点,请注意
[:]

表示拼接:


>>> _list=[1,2,3]
>>> _list[:]=[i+1 for i in _list]
>>> _list
[2, 3, 4]

詹大官人

赞同来自:

>>> [x.__add__/1/ for x in [1, 3, 5]]
3: [2, 4, 6]


我的意图是为了展示元素是否在整数的列表中,它支持各种内置函数。

君笑尘

赞同来自:

Python 2+:


>>> mylist = [1,2,3]
>>> map/lambda x: x + 1, mylist/
[2, 3, 4]


Python 3+:


>>> mylist = [1,2,3]
>>> list/map/lambda x: x + 1, mylist//
[2, 3, 4]

涵秋

赞同来自:

import numpy as np

np.add/[1, 2, 3], 1/.tolist//


是什么赋予了


[2, 3, 4]

石油百科

赞同来自:

我遇到了不是非常有效,而是一种独特的方式。 所以分享它 across.And 是的,它需要另一个列表的额外地点。


from operator import add
test_list1 = [4, 5, 6, 2, 10]
test_list2 = [1] * len/test_list1/

res_list = list/map/add, test_list1, test_list2//

print/test_list1/
print/test_list2/
print/res_list/

#### Output ####
[4, 5, 6, 2, 10]
[1, 1, 1, 1, 1]
[5, 6, 7, 3, 11]

八刀丁二

赞同来自:

list = [1,2,3,4,5]

for index in range/len/list//:
list[index] = list[index] +1

print/list/

石油百科

赞同来自:

上述许多答案非常好。 我也看到了一些奇怪的答案,这将是这项工作。 此外,最后一次看到的响应通过了通常的周期。 这愿意给出答案让我带来
itertools


numpy

, 这将以不同的方式执行相同的工作。

在这里,我提出了各种方式来执行上面没有回答的工作。


import operator
import itertools

x = [3, 5, 6, 7]

integer = 89

"""
Want more vairaint can also use zip_longest from itertools instead just zip
"""
#lazy eval
a = itertools.starmap/operator.add, zip/x, [89] * len/x/// # this is not subscriptable but iterable
print/a/
for i in a:
print/i, end = ","/


# prepared list
a = list/itertools.starmap/operator.add, zip/x, [89] * len/x//// # this returns list
print/a/



# With numpy /before this, install numpy if not present with `pip install numpy`/
import numpy

res = numpy.ones/len/x/, dtype=int/ * integer + x # it returns numpy array
res = numpy.array/x/ + integer # you can also use this, infact there are many ways to play around
print/res/
print/res.shape/ # prints structure of array, i.e. shape

# if you specifically want a list, then use tolist

res_list = res.tolist//
print/res_list/


出口


>>> <itertools.starmap 0x0000028793490af0="" at="" object=""> # output by lazy val
&gt;&gt;&gt; 92,94,95,96, # output of iterating above starmap object
&gt;&gt;&gt; [92, 94, 95, 96] # output obtained by casting to list
&gt;&gt;&gt; __
&gt;&gt;&gt; # |\ | | | |\/| |__| \ /
&gt;&gt;&gt; # | \| |__| | | | |
&gt;&gt;&gt; [92 94 95 96] # this is numpy.ndarray object
&gt;&gt;&gt; /4,/ # shape of array
&gt;&gt;&gt; [92, 94, 95, 96] # this is a list object /doesn't have a shape/


我的

唯一的

强调使用的原因
numpy

在于你应该始终用图书馆制定这样的操作 numpy, 因为它对非常大的阵列有效。
</itertools.starmap>

要回复问题请先登录注册