从另一个列表向列表添加值
Adding values to a list, from another list
我设置了一系列 'for' 循环,现在可以由此确定 3 个值。我现在想将 3 个值存储在列表中。这是我迄今为止尝试过的:
output = [] # creates empty list
k = 0
for i in range(len(GPS[0])-1): # for loop through 1 column of GPS .csv this is the time
for j in range(len(Data[0])-1): # for loop through 1st column of Data .csv this is the time
if GPS[1,i] == Data[1,j]: # if loop, so if time in GPS = time in Data
delta = i-j # finds the difference between values
#print(i,j, delta) # prints row in i that matches row in j, and also the difference between them
#print(GPS[1,i],Data[1,j]) # prints the Height from GPS and time from Data
output[k] = output.append(GPS[1,i], GPS[0,i], Data[1,j])
k=k+1
print(output)
基本上我希望输出是一个包含 3 个值 GPS[1,i]
、GPS[0,i]
和 Data[0,j]
的列表。我试过使用 append 但我似乎无法使用它,我得到的只是 'none'
的列表
这是不对的:
output[k] = output.append(GPS[1,i], GPS[0,i], Data[1,j])
改用output.extend
。所以您的代码可能如下所示:
output = [] # creates empty list
k = 0
for i in range(len(GPS[0])-1): # for loop through 1 column of GPS .csv this is the time
for j in range(len(Data[0])-1): # for loop through 1st column of Data .csv this is the time
if GPS[1,i] == Data[1,j]: # if loop, so if time in GPS = time in Data
delta = i-j # finds the difference between values
#print(i,j, delta) # prints row in i that matches row in j, and also the difference between them
#print(GPS[1,i],Data[1,j]) # prints the Height from GPS and time from Data
output.extend([GPS[1,i], GPS[0,i], Data[1,j]])
k=k+1
print(output)
此外,如果您想将元组附加到输出,您可以 output.append([GPS[1,i], GPS[0,i], Data[1,j]])
代替。
Extend 将添加元素到列表本身
>>> output = []
>>> output.extend([1,2,3])
>>> output
[1, 2, 3]
追加将创建一个新列表并将新列表添加到您的列表
>>> output = []
>>> output.append([1,2,3])
>>> output
[[1, 2, 3]]
您不需要使用 output[k]
,因为 append
本身会将它们添加到 output
。老实说,你甚至不需要 k
因为 k
正好等于 output
.
的长度
append
不能像您尝试的那样接受多个参数。如果你想把三位数据加在一起作为一个元组,使用:
output.append((GPS[1,i], GPS[0,i], Data[1,j]))
编辑:因为您只想将它们添加到列表中,请使用 extend
output.extend([GPS[1,i], GPS[0,i], Data[1,j]])
我设置了一系列 'for' 循环,现在可以由此确定 3 个值。我现在想将 3 个值存储在列表中。这是我迄今为止尝试过的:
output = [] # creates empty list
k = 0
for i in range(len(GPS[0])-1): # for loop through 1 column of GPS .csv this is the time
for j in range(len(Data[0])-1): # for loop through 1st column of Data .csv this is the time
if GPS[1,i] == Data[1,j]: # if loop, so if time in GPS = time in Data
delta = i-j # finds the difference between values
#print(i,j, delta) # prints row in i that matches row in j, and also the difference between them
#print(GPS[1,i],Data[1,j]) # prints the Height from GPS and time from Data
output[k] = output.append(GPS[1,i], GPS[0,i], Data[1,j])
k=k+1
print(output)
基本上我希望输出是一个包含 3 个值 GPS[1,i]
、GPS[0,i]
和 Data[0,j]
的列表。我试过使用 append 但我似乎无法使用它,我得到的只是 'none'
这是不对的:
output[k] = output.append(GPS[1,i], GPS[0,i], Data[1,j])
改用output.extend
。所以您的代码可能如下所示:
output = [] # creates empty list
k = 0
for i in range(len(GPS[0])-1): # for loop through 1 column of GPS .csv this is the time
for j in range(len(Data[0])-1): # for loop through 1st column of Data .csv this is the time
if GPS[1,i] == Data[1,j]: # if loop, so if time in GPS = time in Data
delta = i-j # finds the difference between values
#print(i,j, delta) # prints row in i that matches row in j, and also the difference between them
#print(GPS[1,i],Data[1,j]) # prints the Height from GPS and time from Data
output.extend([GPS[1,i], GPS[0,i], Data[1,j]])
k=k+1
print(output)
此外,如果您想将元组附加到输出,您可以 output.append([GPS[1,i], GPS[0,i], Data[1,j]])
代替。
Extend 将添加元素到列表本身
>>> output = []
>>> output.extend([1,2,3])
>>> output
[1, 2, 3]
追加将创建一个新列表并将新列表添加到您的列表
>>> output = []
>>> output.append([1,2,3])
>>> output
[[1, 2, 3]]
您不需要使用 output[k]
,因为 append
本身会将它们添加到 output
。老实说,你甚至不需要 k
因为 k
正好等于 output
.
append
不能像您尝试的那样接受多个参数。如果你想把三位数据加在一起作为一个元组,使用:
output.append((GPS[1,i], GPS[0,i], Data[1,j]))
编辑:因为您只想将它们添加到列表中,请使用 extend
output.extend([GPS[1,i], GPS[0,i], Data[1,j]])