Python 通过跳过列表之间的值将值附加到空列表
Python append values to empty list by skipping values in between a list
这只是我想要实现的目标的一个最小示例。
我有一个数组:
array = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,....,1,2,3,4,5,6,7,8,9,10]
我想遍历这个数组并创建一个如下所示的新数组:
new_array = [1,1,2,1,2,3,1,2,3,4,1,2,3,4,5,....,1,2,3,4,5,6,7,8,9,10]
即遍历数组 a
,获取第一个值(即 1),然后跳过 remaining 9
值,然后获取第一个和第二个值(即 1, 2)、然后跳过remaining 8
值,依此类推。
我的想法是创建 indices
并按以下方式使用它:
In [1]: indices = np.arange(1,10,1)
Out[1]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
new_array = []
for i in array:
for a,b in zip(indices,range(10)):
new_array.append(i[0:a]) # here I am including i[0:1], i[0:2] and so on
所以它循环 array
并获取第一个值,然后跳过剩余的 9 个值,然后获取前两个值并跳过剩余的 8 个值,依此类推。
但这似乎不起作用。我怎样才能做到这一点?
如果你不需要所有的值(只通过你的方案)
list = [1,2,3,4,5,6,7,8,9,10] * 10
output = []
skip = 9
i = 0
while skip > 0:
output.append(list[i])
i += skip + 1
skip -= 1
print(list)
print(output)
但是你的 "new_array" 没有通过你的算法。为什么不呢:
[1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]
如果我在跳过 9
值后得到第一个 1
(它有索引 0
),我将得到 1
,然后跳过 8
值我不会得到2
编辑: 好的,我现在明白了。这应该有效:
list = [1,2,3,4,5,6,7,8,9,10] * 10
output = []
skip = 9
i = 0
j = 0
add = 1
while skip >= 0:
newList = list[i:j+1]
for x in newList:
output.append(x)
i += skip + add
j += skip + add + 1
add += 1
skip -= 1
print(output)
你能试试这个代码吗?我已经在 python 3 上测试了这段代码,它工作正常。
inp = [1,2,3,4,5,6,7,8,9,10] * 10
inp_len = len(inp);
output = [inp[0]]
skip = 9
limit= skip +2;
pointer = 1;
while skip > 0:
pointer = pointer+skip;
if(pointer >inp_len):
pointer = pointer %inp_len;
for x in inp[pointer : pointer+limit-skip ]:
output.append(x);
pointer= pointer+ limit-skip ;
skip=skip-1;
print(inp)
print(output)
说明 - 添加默认的第一个元素,然后按以下顺序添加元素。
- 跳过 9 个元素 = [1, 2]
- 跳过 8 个元素 = [1, 2, 3]
- 跳过 7 个元素 = [1, 2, 3, 4]
- 跳过 6 个元素 = [1, 2, 3, 4, 5]
- 跳过 5 个元素 = [1, 2, 3, 4, 5, 6]
- 跳过 4 个元素 = [1, 2, 3, 4, 5, 6, 7]
- 跳过 3 个元素 = [1, 2, 3, 4, 5, 6, 7, 8]
- 跳过 2 个元素 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
- 跳过 1 个元素 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
请根据您的输入进行测试。我在这里使用定义的列表。
输入列表 -[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10]
输出列表 - [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6 , 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10]
怎么样:
list = [1,2,3,4,5,6,7,8,9,10] * 10;
output = [];
take = 1;
index = 0;
while index < len(list):
# take the "take" next elements, notice the "index" is not changing.
for i in range(take):
output.append(list[index + i]);
# Skip the remaining values and increase the "take"
index += 10;
take += 1;
print(list)
print(output)
你也可以这样使用两个索引:
list = [1,2,3,4,5,6,7,8,9,10] * 10;
output = [];
index = 0;
for i in range(10):
for j in range(10):
if j <= i:
output.append(list[index + i]);
index++;
print(list)
print(output)
这与用 1 到 10 的数字打印三角形并没有太大区别。
array = [x for x in range(1,11)]*10 #here 10 means adding same list 10 times
new_array = []
for i in range(10):
new_array += array[10*i:10*i+i+1]
print(new_array)
希望对您有所帮助!
需要 0.0005 秒
对于单个列表,您还可以为此使用列表扩展:
list = [1,2,3,4,5,6,7,8,9,10]
output = []
i = 1
while i <= 10:
output.extend(list[0:i])
i +=1
print output
对于您的列表,您可以将其扩展为:
list = [1,2,3,4,5,6,7,8,9,10]*10
output = []
i = 1
j = 0
k = 1
while k <= 10:
output.extend(list[j:i])
j +=10
k +=1
i = j+k
print output
array = [1,2,3,4,5,6,7,8,9,10]*10
new_array = []
c=j=0
while c < len(array):
for i in range(0,j):
new_array.append(array[i])
i+=1
j+=1
c+=10
print(new_array)
输出
[1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9]
这只是我想要实现的目标的一个最小示例。
我有一个数组:
array = [1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,....,1,2,3,4,5,6,7,8,9,10]
我想遍历这个数组并创建一个如下所示的新数组:
new_array = [1,1,2,1,2,3,1,2,3,4,1,2,3,4,5,....,1,2,3,4,5,6,7,8,9,10]
即遍历数组 a
,获取第一个值(即 1),然后跳过 remaining 9
值,然后获取第一个和第二个值(即 1, 2)、然后跳过remaining 8
值,依此类推。
我的想法是创建 indices
并按以下方式使用它:
In [1]: indices = np.arange(1,10,1)
Out[1]: array([1, 2, 3, 4, 5, 6, 7, 8, 9])
new_array = []
for i in array:
for a,b in zip(indices,range(10)):
new_array.append(i[0:a]) # here I am including i[0:1], i[0:2] and so on
所以它循环 array
并获取第一个值,然后跳过剩余的 9 个值,然后获取前两个值并跳过剩余的 8 个值,依此类推。
但这似乎不起作用。我怎样才能做到这一点?
如果你不需要所有的值(只通过你的方案)
list = [1,2,3,4,5,6,7,8,9,10] * 10
output = []
skip = 9
i = 0
while skip > 0:
output.append(list[i])
i += skip + 1
skip -= 1
print(list)
print(output)
但是你的 "new_array" 没有通过你的算法。为什么不呢:
[1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10,1,2,3,4,5,6,7,8,9,10]
如果我在跳过 9
值后得到第一个 1
(它有索引 0
),我将得到 1
,然后跳过 8
值我不会得到2
编辑: 好的,我现在明白了。这应该有效:
list = [1,2,3,4,5,6,7,8,9,10] * 10
output = []
skip = 9
i = 0
j = 0
add = 1
while skip >= 0:
newList = list[i:j+1]
for x in newList:
output.append(x)
i += skip + add
j += skip + add + 1
add += 1
skip -= 1
print(output)
你能试试这个代码吗?我已经在 python 3 上测试了这段代码,它工作正常。
inp = [1,2,3,4,5,6,7,8,9,10] * 10
inp_len = len(inp);
output = [inp[0]]
skip = 9
limit= skip +2;
pointer = 1;
while skip > 0:
pointer = pointer+skip;
if(pointer >inp_len):
pointer = pointer %inp_len;
for x in inp[pointer : pointer+limit-skip ]:
output.append(x);
pointer= pointer+ limit-skip ;
skip=skip-1;
print(inp)
print(output)
说明 - 添加默认的第一个元素,然后按以下顺序添加元素。
- 跳过 9 个元素 = [1, 2]
- 跳过 8 个元素 = [1, 2, 3]
- 跳过 7 个元素 = [1, 2, 3, 4]
- 跳过 6 个元素 = [1, 2, 3, 4, 5]
- 跳过 5 个元素 = [1, 2, 3, 4, 5, 6]
- 跳过 4 个元素 = [1, 2, 3, 4, 5, 6, 7]
- 跳过 3 个元素 = [1, 2, 3, 4, 5, 6, 7, 8]
- 跳过 2 个元素 = [1, 2, 3, 4, 5, 6, 7, 8, 9]
- 跳过 1 个元素 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
请根据您的输入进行测试。我在这里使用定义的列表。
输入列表 -[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6 , 7, 8, 9, 10]
输出列表 - [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6 , 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9, 1 , 2, 3, 4, 5, 6, 7, 8, 9, 10]
怎么样:
list = [1,2,3,4,5,6,7,8,9,10] * 10;
output = [];
take = 1;
index = 0;
while index < len(list):
# take the "take" next elements, notice the "index" is not changing.
for i in range(take):
output.append(list[index + i]);
# Skip the remaining values and increase the "take"
index += 10;
take += 1;
print(list)
print(output)
你也可以这样使用两个索引:
list = [1,2,3,4,5,6,7,8,9,10] * 10;
output = [];
index = 0;
for i in range(10):
for j in range(10):
if j <= i:
output.append(list[index + i]);
index++;
print(list)
print(output)
这与用 1 到 10 的数字打印三角形并没有太大区别。
array = [x for x in range(1,11)]*10 #here 10 means adding same list 10 times
new_array = []
for i in range(10):
new_array += array[10*i:10*i+i+1]
print(new_array)
希望对您有所帮助!
需要 0.0005 秒
对于单个列表,您还可以为此使用列表扩展:
list = [1,2,3,4,5,6,7,8,9,10]
output = []
i = 1
while i <= 10:
output.extend(list[0:i])
i +=1
print output
对于您的列表,您可以将其扩展为:
list = [1,2,3,4,5,6,7,8,9,10]*10
output = []
i = 1
j = 0
k = 1
while k <= 10:
output.extend(list[j:i])
j +=10
k +=1
i = j+k
print output
array = [1,2,3,4,5,6,7,8,9,10]*10
new_array = []
c=j=0
while c < len(array):
for i in range(0,j):
new_array.append(array[i])
i+=1
j+=1
c+=10
print(new_array)
输出
[1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 6, 1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 9]