繁体   English   中英

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 ,遍历数组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、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]

对于清单列表,您还可以为此使用列表扩展名:

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   

怎么样:

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秒

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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM