[英]Python append values to empty list by skipping values in between a list
Just a minimal example of what I want to achieve. 只是我想要实现的最小示例。
I have an array: 我有一个数组:
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]
I would like to loop through this array and create a new array which looks like this: 我想遍历此数组并创建一个新的数组,如下所示:
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]
ie loop through array a
, get the first value (ie 1), then skip the remaining 9
values , then get the first and the second value (ie 1,2), then skip the remaining 8
values , and so on. a
,遍历数组a
,获取第一个值(即1), 然后跳过remaining 9
值 ,然后获取第一个和第二个值(即1,2), 然后跳过remaining 8
值 ,依此类推。
The idea I came up with was to create indices
and use it in the following way: 我想到的想法是创建
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
So it loops through array
and gets the first value, then skips the remaining 9 values, then gets the first two values and skips the remaining 8 values and so on. 因此,它遍历
array
并获取第一个值,然后跳过其余的9个值,然后获取前两个值并跳过其余的8个值,依此类推。
But this doesn't seem to work. 但这似乎不起作用。 How can I achieve this ?
我该如何实现?
If you don't need all values (only pass your scheme) 如果不需要所有值(仅通过方案)
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)
But your "new_array" doesn't pass your algorithm. 但是您的“ new_array”没有通过您的算法。 Why not:
为什么不:
[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]
If I get first 1
(it has index 0
) after skip 9
values I will get 1
, after then skipping 8
values I won't get 2
[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
Edit: Ok, I understand now. 编辑:好的,我现在明白了。 This should work:
这应该工作:
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)
can you please try this code. 你能试试这个代码吗 I have tested this code on python 3 and it is working fine.
我已经在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)
Explaination - Adding default first element and then adding elements in below order. 说明-添加默认的第一个元素,然后按以下顺序添加元素。
Please test it with your input . 请根据您的输入进行测试。 Here i am using a defined list.
我在这里使用定义的列表。
Input list -[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、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]
Output list - [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] 输出列表-[1、2、1、2、1、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]
For a signle list you can also use list extension for this: 对于清单列表,您还可以为此使用列表扩展名:
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
For your list you can extend this to: 对于您的列表,您可以将其扩展为:
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
How about: 怎么样:
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)
You can also go like this with two indices: 您也可以像这样使用两个索引:
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)
it's not too different from printing a triangle with numbers from 1 to 10. 它与打印数字从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)
hope this helps! 希望这可以帮助!
It takes 0.0005 sec 耗时0.0005秒
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)
Output 输出量
[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]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.