import numpy as np
c=[4,8,2,3........]
a=np.array([2,3,5],np.int16) #data type=np.int16=Integer(-32768 +32767))
for i in range(len(c)):
a.append(c[i]) # This line getting error
如何使用append或任何其他方法为此整数数组“a”添加一些值?
答案 0 :(得分:3)
您收到错误的原因是您无法附加到numpy.array
。如果a
是list
,那么您就没有问题了。
看起来这就是你要找的东西:
In [4]: a = [2,3,5]
In [5]: c = [4,8,2,3]
In [6]: a.extend(c)
In [7]: a
Out[7]: [2, 3, 5, 4, 8, 2, 3]
如果a
和c
为numpy.arrays
,您可以:
In [12]: a = np.array([2,3,5])
In [13]: c = np.array([4,8,2,3])
In [14]: np.concatenate((a,c))
Out[14]: array([2, 3, 5, 4, 8, 2, 3])
答案 1 :(得分:0)
好的,根据文档,numpy数组没有追加功能。
以下是对代码的更正:
import numpy as np
c=[4,8,2,3........]
a=np.array([2,3,5],np.int16) #data type=np.int16=Integer(-32768 +32767))
for i in range(len(c)):
a = np.append(a, c[i]) # This line getting error
我想这就是你想要做的,根据文档numpy.append()是一个你可以使用的自由函数。我希望这会对你有所帮助。
答案 2 :(得分:0)
列表在项目构造方面比numpy数组更有效。如果您想逐个append
,我建议您附加一个列表,然后使用np.array(myList)
将其转换为numpy数组。对代码执行相同操作的更快方法是:
a = np.array(list(a) + c, np.int16)
如果您只想将c
列表转换为int16
数组:
c = np.array(c, np.int16)
然后根据@Akavall你可以np.concatenate
使用或者使用更紧凑的np.r_
表格(注意方括号)。
a = np.r_[a,c]
答案 3 :(得分:0)
这里有一些timeit代码,用于比较将2D Python小整数列表转换为1D Numpy数组的3种不同方式的速度。
直接的方法是简单地将整个列表传递给Numpy的array
函数,然后通过其.flatten
方法将生成的2D数组展平为1D。这是grid_to_array
采取的方法。
我测试的其他方法都使用numpy.append
函数。 grid_rows_to_array
逐行构建数组,grid_items_to_array
逐项构建数组。正如您可能猜到的,最后一种方法非常慢。即使对于10x10列表,它也比grid_to_array
慢大约50倍。对于较大的列表,它的速度确实是冰河。
#!/usr/bin/env python3
''' Compare the speeds of various functions that convert a
2D integer list to a 1D Numpy array.
See https://stackoverflow.com/q/44512661/4014959
Written by PM 2Ring 2017.06.13
'''
import numpy as np
from timeit import Timer
def make_grid(n):
''' Make a 2D list of integers '''
return [list(range(i, i + n)) for i in range(0, n * n, n)]
# The functions to test
def grid_to_array(g):
''' Create a 2D array from the whole grid and convert it to 1D '''
return np.array(g).flatten()
def grid_rows_to_array(g):
''' Create a 1D array from the 1st row of the grid,
then append all the other rows to it, row by row
'''
# An iterator that yields the rows
it = iter(g)
a = np.array(next(it))
for row in it:
a = np.append(a, row)
return a
def grid_items_to_array(g):
''' Create an array from the 1st item of the grid,
then append all the other items to it, item by item
'''
# A generator that yields the items
gen = (u for row in g for u in row)
a = np.array(next(gen))
for u in gen:
a = np.append(a, u)
return a
funcs = (
grid_to_array,
grid_rows_to_array,
grid_items_to_array,
)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
def verify(size):
''' Verify that all the functions give the same result '''
print('Grid rows, size=', size)
grid = make_grid(size)
for row in grid:
print(row)
print()
results = []
for func in funcs:
print(func.__name__)
a = func(grid)
print(a, '\n')
results.append(a)
# Test that all arrays are identical
first, *results = results
rc = all((first == u).all() for u in results)
print('all ok' if rc else 'Error!')
return rc
def time_test(loops, reps):
''' Print timing stats for all the functions '''
timings = []
for func in funcs:
fname = func.__name__
setup = 'from __main__ import grid, ' + fname
cmd = fname + '(grid)'
t = Timer(cmd, setup)
result = t.repeat(reps, loops)
result.sort()
timings.append((result, fname))
timings.sort()
for result, fname in timings:
print('{:20} {}'.format(fname, result))
verify(5)
# Do the timing tests
reps = 3
loops = 128
for i in range(6):
size = 10 * (2 ** i)
grid = make_grid(size)
print('\n{0}: Size={1}, Loops={2}'.format(i, size, loops))
time_test(loops, reps)
loops >>= 1
<强>输出强>
Grid rows, size= 5
[0, 1, 2, 3, 4]
[5, 6, 7, 8, 9]
[10, 11, 12, 13, 14]
[15, 16, 17, 18, 19]
[20, 21, 22, 23, 24]
grid_to_array
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
grid_rows_to_array
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
grid_items_to_array
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
all ok
0: Size=10, Loops=128
grid_to_array [0.007200194995675702, 0.01232630800222978, 0.014947812996979337]
grid_rows_to_array [0.047056858995347284, 0.04709799499687506, 0.047196302002703305]
grid_items_to_array [0.4025771490050829, 0.40894141500029946, 0.4112219040034688]
1: Size=20, Loops=64
grid_to_array [0.009614694004994817, 0.01012843299395172, 0.015028686997538898]
grid_rows_to_array [0.0559136279989616, 0.05666288200154668, 0.08348615399881965]
grid_items_to_array [0.8405412119973334, 0.8444936599989887, 0.8777696510005626]
2: Size=40, Loops=32
grid_to_array [0.018018493996351026, 0.019878495004377328, 0.023297555999306496]
grid_rows_to_array [0.06234459100232925, 0.06246465100412024, 0.06281185599800665]
grid_items_to_array [1.7235709279993898, 1.7587534330014023, 1.8295516790021793]
3: Size=80, Loops=16
grid_to_array [0.029057150000880938, 0.03226659999927506, 0.03832875600346597]
grid_rows_to_array [0.08397659000183921, 0.08470724899962079, 0.0852264919958543]
grid_items_to_array [3.7167786709978827, 3.8199421919998713, 4.046950017000199]
4: Size=160, Loops=8
grid_to_array [0.06241827599296812, 0.06254585299757309, 0.07094153799698688]
grid_rows_to_array [0.14922553599899402, 0.1522045050005545, 0.15509943600045517]
grid_items_to_array [11.045316871000978, 11.064624926999386, 12.66077643600147]
5: Size=320, Loops=4
grid_to_array [0.13331966300029308, 0.1439433339983225, 0.15014286800578702]
grid_rows_to_array [0.6806031250016531, 0.685745176000637, 0.7797461770023801]
grid_items_to_array [162.62937470299948, 162.73040329200012, 167.80105410800024]
这些时序是使用Python 3.6.0在相当古老的2GHz单核32位机器上获得的,该机器具有2GB RAM,运行Linux的Debian衍生物。 YMMV。