python for循环中的数组问题

unhi4e5o  于 2023-02-15  发布在  Python
关注(0)|答案(9)|浏览(215)

我想从10个元素的数组A到100个元素的数组B。
从0到9的B的每个元素等于A的元素0
从10到19的B的每个元素等于A的元素1
....
从90到99的B的每个元素等于A的元素9
我做了下面的代码,但它不工作

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
A = np.asarray(a)

b = []
for i in range(len(A)*10):
    b.append(0)
    
B = np.asarray(b)   

for i in range(len(A)):
    for j in range(9):
        B[j]=A[i]

预期结果:

B [ 0,0,0,0,0,0,0,0,0,0,
  1,1,1,1,1,1,1,1,1,1,
  2,2,2,2,2,2,2,2,2,2
  ...,
  9,9,9,9,9,9,9,9,9,9 ]
2vuwiymt

2vuwiymt1#

您只保存了前9个列表元素中的值。您必须通过将i*10添加到索引中来“缩放”它。

import numpy as np

a=[0, 1, 2, 3, 4, 5, 6, 7]
A = np.asarray(a)

b = []
for i in range(len(A)**2):
    b.append(0)
    
B = np.asarray(b)   

for i in range(len(A)):
    for j in range(len(A)):
        B[j + i*len(A)]=A[i]

print(B)
wz1wpwve

wz1wpwve2#

这对我很有效:

>>> a = [1,2,3]
>>> [ x for i in a for x in [i]*3]
[1, 1, 1, 2, 2, 2, 3, 3, 3]
>>>

您可以将3替换为10或任何您喜欢的内容。
回答雅各布的问题:

>>> [[a]*10 for a in A]
[[1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [2, 2, 2, 2, 2, 2, 2, 2, 2, 2], [3, 3, 3, 3, 3, 3, 3, 3, 3, 3]]
kqlmhetl

kqlmhetl3#

你应该尽可能地避免numpy循环,因为这会让你失去意义,这里你可以使用repeat()

import numpy as np

a=[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
A = np.asarray(a)
B = A.repeat(10)

乙:

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2,
       2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4,
       4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6,
       6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 8, 8, 8, 8, 8, 8, 8, 8,
       8, 8, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9])

如果想要一个嵌套列表,只需重新整形:

B = A.repeat(10).reshape(-1, 10)

array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
       [1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
       [2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
       [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
       [4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
       [5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
       [6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
       [7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
       [8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
       [9, 9, 9, 9, 9, 9, 9, 9, 9, 9]])
but5z9lq

but5z9lq4#

可以使用numpy并指定每个元素的迭代次数:

import numpy as np
A = [1,2,3,4]
B = [np.full(10, a) for a in A]
print(B)

或者,如果您不喜欢使用numpy,请用途:

A = [1,2,3,4]
B = [[a]*10 for a in A]
print(B)

给你通缉名单B

i2byvkas

i2byvkas5#

试试这个:

a = [*range(10)]
b = []
for i in range(10):
    b.extend([a[i]* 10])
B = np.asarray(b)
lhcgjxsq

lhcgjxsq6#

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = []
for x in a:
    b += [x] * 10
print b

这个答案更好,来自lenik的想法

a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b = [x for x in a for i in range(10)]
print b
2skhul33

2skhul337#

用一行字回答:print([item for sublist in [[i]*10 for i in range(1,10)] for item in sublist])

kx1ctssn

kx1ctssn8#

如果a是泛型列表而不是有序序列

In [20]: a = [1, 'a', 3.14159, False, {1:2, 3:4}]

您可以执行以下操作

In [21]: [_ for _ in (zip(*(a for _ in a))) for _ in _]
Out[21]: 
[1,
 1,
 1,
 1,
 1,
 'a',
 'a',
 'a',
 'a',
 'a',
 3.14159,
 3.14159,
 3.14159,
 3.14159,
 3.14159,
 False,
 False,
 False,
 False,
 False,
 {1: 2, 3: 4},
 {1: 2, 3: 4},
 {1: 2, 3: 4},
 {1: 2, 3: 4},
 {1: 2, 3: 4}]
s71maibg

s71maibg9#

list1=[]
list2=[]
for i in range (0,10,1):
    list1.append(i)
print(list1)
for i in range (0,10,1):
    for j in range (0,10,1):
        j=i
        list2.append(j)
print(list2)

相关问题