如何在python中对列表元素进行数学运算?
假设我有一个数字列表如何在python中对列表元素进行数学运算?,python,list,loops,operations,Python,List,Loops,Operations,假设我有一个数字列表[3,51,34]。我想向每个元素添加以前元素的总和,并返回一个包含这些新值的新列表。 所以这里的结果是[3,54,88]。通常如何在任意大小的输入列表上执行此操作?此代码的最后一行应该适用于已知的大小列表 indices1 = range(len(list1)) indices1.sort(key=lambda x: list2[x]) list1 = map(lambda i: list1[i], indices1) labelled = zip(list1, asci
[3,51,34]
。我想向每个元素添加以前元素的总和,并返回一个包含这些新值的新列表。
所以这里的结果是[3,54,88]
。通常如何在任意大小的输入列表上执行此操作?此代码的最后一行应该适用于已知的大小列表
indices1 = range(len(list1))
indices1.sort(key=lambda x: list2[x])
list1 = map(lambda i: list1[i], indices1)
labelled = zip(list1, ascii_uppercase)
sorted_data = sorted(labelled, key=itemgetter(0))
labels = [pair[1] for pair in sorted_data]
newlist, = [ 0, list1[0], list1[1] + list1[2], list1[0] + list[1] + list[2]]
numpy.cumsum
对于这样的东西可能是个不错的选择
In [1]: import numpy as np
In [2]: a = [3,51,34]
In [3]: np.cumsum(a)
Out[3]: array([ 3, 54, 88])
numpy.cumsum
对于这样的东西可能是个不错的选择
In [1]: import numpy as np
In [2]: a = [3,51,34]
In [3]: np.cumsum(a)
Out[3]: array([ 3, 54, 88])
一个简单的方法是:
nums = [3,51,34]
reduce(lambda x, y: [y] if not x else x + [y + x[-1]], nums, None)
# [3, 54, 88]
一个简单的方法是:
nums = [3,51,34]
reduce(lambda x, y: [y] if not x else x + [y + x[-1]], nums, None)
# [3, 54, 88]
为了完整性,还有一个简单的程序解决方案:
a = [3,51,34]
def cumsum(numbers):
accum = 0
result = []
for n in numbers:
accum += n
result.append(accum)
return result
print cumsum(a)
它打印了
[3,54,88]
和一个简单的程序解决方案,以确保完整性:
a = [3,51,34]
def cumsum(numbers):
accum = 0
result = []
for n in numbers:
accum += n
result.append(accum)
return result
print cumsum(a)
它打印一个列表理解方法,因为这总是很有趣的:
>>> data = [3, 51, 34]
>>> result = [n + sum(data[:i]) for i, n in enumerate(data)]
>>> result
[3, 54, 88]
列表理解法,因为这总是很有趣:
>>> data = [3, 51, 34]
>>> result = [n + sum(data[:i]) for i, n in enumerate(data)]
>>> result
[3, 54, 88]
基于生成器的解决方案
In [25]: ll = [3,51,34]
In [26]: def acc(myiter):
....: it = iter(myiter)
....: total = it.next()
....: yield total
....: for element in it:
....: total = total + element
....: yield total
....:
In [27]: acc(ll)
Out[27]: <generator object acc at 0x1ec9e10>
In [28]: [x for x in acc(ll)]
Out[28]: [3, 54, 88]
基于生成器的解决方案
In [25]: ll = [3,51,34]
In [26]: def acc(myiter):
....: it = iter(myiter)
....: total = it.next()
....: yield total
....: for element in it:
....: total = total + element
....: yield total
....:
In [27]: acc(ll)
Out[27]: <generator object acc at 0x1ec9e10>
In [28]: [x for x in acc(ll)]
Out[28]: [3, 54, 88]
我想对第一个列表做一些处理&第二个列表将是输出,我在IDE上尝试了这段代码,因为太多了errors@user3549209你能提供你正在使用的输入吗?@user3549209,
np.cumsum(a)
返回一个np.array
,如果你想要一个列表
,你可以只做list(np.cumsum(a))
。我想对第一个列表进行一些处理&第二个列表将作为输出,我在IDE上尝试了这段代码,因为太多了errors@user3549209你能提供你正在使用的输入吗?@user3549209,np.cumsum(a)
返回一个np.array
,如果你想要一个列表
,你可以只做list(np.cumsum(a))
。我在您的代码中看到您在结果列表的开头添加了一个[0]。在这种情况下,reduce将是reduce(lambda x,y:x+[y+x[-1]],nums,[0])
(打印[0,3,54,88]
)我没有添加0,实际上第一个元素设置为零,所以?我从newlist,=[0,
,但如果不正确,请忽略它。因为在原始代码中,您实际上是在输出列表的开头添加了一个0。list1
包含3个元素,newList
包含4个元素,第一个元素是0。这就是为什么。我在您的代码中看到您添加了一个[0]在结果列表的开始处。在这种情况下,reduce将是reduce(lambda x,y:x+[y+x[-1]],nums,[0])
(打印[0,3,54,88]
)我没有添加0,实际上第一个元素设置为零,因此?我从newlist中推断,=[0,
,但如果不正确,请忽略它。因为在原始代码中,您实际上是在输出列表的开头添加了一个0。list1
包含3个元素,newList
包含4个元素,第一个元素是0。这就是原因。