Sum one number to every element in a list (or array) in Python(将一个数字与 Python 中列表(或数组)中的每个元素相加)
本文介绍了将一个数字与 Python 中列表(或数组)中的每个元素相加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在这里我再次提出我的基本问题,但请耐心等待.
Here I go with my basic questions again, but please bear with me.
在 Matlab 中,向列表中的元素添加数字相当简单:
In Matlab, is fairly simple to add a number to elements in a list:
a = [1,1,1,1,1]
b = a + 1
b
那么是 [2,2,2,2,2]
在 python 中,这似乎不起作用,至少在一个列表中.
In python this doesn't seem to work, at least on a list.
有没有一种简单快速的方法可以将单个数字添加到整个列表中.
Is there a simple fast way to add up a single number to the entire list.
谢谢
推荐答案
如果你想对数字列表进行操作,最好使用 NumPy 数组:
if you want to operate with list of numbers it is better to use NumPy arrays:
import numpy
a = [1, 1, 1 ,1, 1]
ar = numpy.array(a)
print ar + 2
给予
[3, 3, 3, 3, 3]
这篇关于将一个数字与 Python 中列表(或数组)中的每个元素相加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:将一个数字与 Python 中列表(或数组)中的每个元素


猜你喜欢
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- 我如何透明地重定向一个Python导入? 2022-01-01