Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在列表中添加相邻的数字?我无法获取最后一个变量_Python_Loops - Fatal编程技术网

Python 如何在列表中添加相邻的数字?我无法获取最后一个变量

Python 如何在列表中添加相邻的数字?我无法获取最后一个变量,python,loops,Python,Loops,设x=[3,8,-2,6,9,-4,7,1,-5,8] 使用for循环在x中添加相邻元素。 将每个结果存储在向量sa中。 同时显示矢量x和矢量sa。 例如,sa的前3个编号为: sa = [11, 9, 12, …] = [(3+8), (3+8+(-2)), (8+(-2)+6), …] 我有这样的东西 x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8] sa = [] for i in range (0, len(x)-1): if i<1:

设x=[3,8,-2,6,9,-4,7,1,-5,8]

使用for循环在x中添加相邻元素。 将每个结果存储在向量sa中。 同时显示矢量x和矢量sa。 例如,sa的前3个编号为:

sa = [11, 9, 12, …]  = [(3+8), (3+8+(-2)), (8+(-2)+6), …] 
我有这样的东西

x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]
sa = []

for i in range (0, len(x)-1):
    if i<1:
        sa.append(x[0] + x[1])
    elif i >= 1 & i< len(x):
        sa.append(x[i-1] + x[i] + x[i+1])
    if i == 0:
        sa.append(x[i] + x[i+1])
print("Here is sa", sa) 
甚至

[11, 9, 12, 13, 11, 12, 4, 3, 4, 3]

您可以将其写为列表理解,注意sa中的第i个元素是x[i-1]到x[i+1]的x值之和。现在,这些索引可能会超出x的边界,因此我们需要使用max和min将它们保持在范围内:

x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]

sa = [sum(x[max(i-1, 0):min(i+2, len(x))]) for i in range(len(x))]
print(sa)
输出:

[11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
Here is sa [11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
Here is sa [11, 9, 12, 13, 11, 12, 4, 3, 4, 3]

您可以将其写为列表理解,注意sa中的第i个元素是x[i-1]到x[i+1]的x值之和。现在,这些索引可能会超出x的边界,因此我们需要使用max和min将它们保持在范围内:

x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]

sa = [sum(x[max(i-1, 0):min(i+2, len(x))]) for i in range(len(x))]
print(sa)
输出:

[11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
Here is sa [11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
Here is sa [11, 9, 12, 13, 11, 12, 4, 3, 4, 3]

以防万一,如果你正在寻找非全面的方式,这里就是

for i in range(len(x)):
    if i == 0:
        sa.append(x[i] + x[i+1])
    elif 1 <= i < len(x)-1:
        sa.append(x[i-1] + x[i] + x[i+1])
    else:
        sa.append(x[i] + x[i-1])

print("Here is sa", sa)

以防万一,如果你正在寻找非全面的方式,这里就是

for i in range(len(x)):
    if i == 0:
        sa.append(x[i] + x[i+1])
    elif 1 <= i < len(x)-1:
        sa.append(x[i-1] + x[i] + x[i+1])
    else:
        sa.append(x[i] + x[i-1])

print("Here is sa", sa)

您的代码有一些错误:

x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]
sa = []

# the last element of the loop will be x[len(x)-2], it is -5 in this case
# -5 is the last element this loop process, it adds up 1, -5 and 8
# this loop will not take 8 as the central number, so the results miss the last value
for i in range (0, len(x)-1):
    if i<1:
        sa.append(x[0] + x[1])
    # in python, it is `and` not &, & is bitwise and.
    elif i >= 1 & i< len(x):
        sa.append(x[i-1] + x[i] + x[i+1])
    if i == 0: # this is meaningless, i == 0 is covered in the first if branch
        sa.append(x[i] + x[i+1])
print("Here is sa", sa)

您的代码有一些错误:

x = [3, 8, -2, 6, 9, -4, 7, 1, -5, 8]
sa = []

# the last element of the loop will be x[len(x)-2], it is -5 in this case
# -5 is the last element this loop process, it adds up 1, -5 and 8
# this loop will not take 8 as the central number, so the results miss the last value
for i in range (0, len(x)-1):
    if i<1:
        sa.append(x[0] + x[1])
    # in python, it is `and` not &, & is bitwise and.
    elif i >= 1 & i< len(x):
        sa.append(x[i-1] + x[i] + x[i+1])
    if i == 0: # this is meaningless, i == 0 is covered in the first if branch
        sa.append(x[i] + x[i+1])
print("Here is sa", sa)
创建一个包含前两个元素之和的列表

>>> q = [sum(x[:2])]
一次遍历三个列表并追加总和

>>> for thing in zip(x,x[1:],x[2:]):
...     q.append(sum(thing))
追加最后两项的总和

>>> q.append(sum(x[-2:]))

>>> q
[11, 9, 12, 13, 11, 12, 4, 3, 4, 3]
创建一个包含前两个元素之和的列表

>>> q = [sum(x[:2])]
一次遍历三个列表并追加总和

>>> for thing in zip(x,x[1:],x[2:]):
...     q.append(sum(thing))
追加最后两项的总和

>>> q.append(sum(x[-2:]))

>>> q
[11, 9, 12, 13, 11, 12, 4, 3, 4, 3]

嘿,威廉,你能编辑你的问题来确切地指出在你的代码运行过程中会发生什么吗?你可以在C++中使用矢量。@ DavidSmolinski这个问题是关于Python……你也可以使用NoMPy来获取矢量。@ DavidSmolinski或者你可以只用Python列表…它本质上等同于C++向量。NUMPY实际上提供了真正的多维数组,不像C++向量,我相信会使用向量威廉你能编辑你的问题来确切地指出当你运行代码时会发生什么吗?你可以在C++中使用矢量。@ DavidSmolinski这个问题是关于Python……你也可以使用NUMPY来获取矢量。@ DavidSmolinski或者你可以只用Python列表…它本质上等同于C++向量。NUMPY实际上提供了真正的多维数组,不像C++向量,我相信它会使用向量向量。