import numpy as np
a = np.array([[0,0,0],[10,10,10],[20,20,20],[30,30,30]])
b = np.array([0,1,2])print(a + b)
4.迭代数组
NumPy 迭代器对象 numpy.nditer 提供了一种灵活访问一个或者多个数组元素的方式
import numpy as np
a = np.arange(6).reshape(2,3)print('原始数组是:')print(a)print('\n')print('迭代输出元素:')for x in np.nditer(a):print(x, end=", ")print('\n')
控制遍历顺序
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)print('原始数组是:')print(a)print('\n')print('原始数组的转置是:')
b = a.T
print(b)print('\n')print('以 C 风格顺序排序:')
c = b.copy(order='C')print(c)for x in np.nditer(c):print(x, end=", ")print('\n')print('以 F 风格顺序排序:')
c = b.copy(order='F')print(c)for x in np.nditer(c):print(x, end=", ")
修改数组中元素的值
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)print('原始数组是:')print(a)print('\n')for x in np.nditer(a, op_flags=['readwrite']):
x[...]=2* x
print('修改后的数组是:')print(a)
迭代器遍历对应于每列,并组合为一维数组
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)print('原始数组是:')print(a)print('\n')print('修改后的数组是:')for x in np.nditer(a, flags =['external_loop'], order ='F'):print(x, end=", ")
5.广播迭代
如果两个数组是可广播的,nditer 组合对象能够同时迭代它们。
import numpy as np
a = np.arange(0,60,5)
a = a.reshape(3,4)print('第一个数组为:')print(a)print('\n')print('第二个数组为:')
b = np.array([1,2,3,4], dtype =int)print(b)print('\n')print('修改后的数组为:')for x,y in np.nditer([a,b]):print("%d:%d"%(x,y), end=", ")
比赛链接:Dashboard - Codeforces Round #847 (Div. 3) - Codeforces
目录 A. Polycarp and the Day of Pi
B. Taisia and Dice
C. Premutation
D. Matryoshkas
E. Vlad and a Pair of Numbers A. Polycarp and the Day of Pi 题意:求出一个数字…
Target塔吉特是美国仅次于Walmart沃尔玛的第二大巨型折扣零售百货集团,由于拓展了其数字化履约能力,使得越来越多的国内零售产品供应商和Target建立合作关系。Target要求其供应商通过EDI(Electronic Data Interchange,中文名称是电…