python:测试数组并打印名称
我正在分析制造过程中元素依赖性的设计结构矩阵(DSM)。经过一些矩阵运算后,我得到两个数组,它们是结果矩阵的列和行的总和:python:测试数组并打印名称,python,arrays,numpy,indexing,Python,Arrays,Numpy,Indexing,我正在分析制造过程中元素依赖性的设计结构矩阵(DSM)。经过一些矩阵运算后,我得到两个数组,它们是结果矩阵的列和行的总和: SOC = array([1, 2, 2, 3, 3, 4]) SOR = array([6, 2, 3, 1, 2, 1]) 每个数组中的相应位置被索引到以下六个项目的数组中(对应于原始设计结构(I/O)矩阵中的项目) 然后,我想测试基于四个标准规则的每个项,并将它们排序到另外四个数组中 cr = [] sh = [] pr = [] ct = [] result =
SOC = array([1, 2, 2, 3, 3, 4])
SOR = array([6, 2, 3, 1, 2, 1])
每个数组中的相应位置被索引到以下六个项目的数组中(对应于原始设计结构(I/O)矩阵中的项目)
然后,我想测试基于四个标准规则的每个项,并将它们排序到另外四个数组中
cr = []
sh = []
pr = []
ct = []
result = [cr, sh, pr, ct]
for i in ITEM[0:5]:
if i[0]>3 and i[1]>3:
cr.append(i)
if i[0]>3 and i[1]<=3:
sh.append(i)
if i[0]<=3 and i[1]<=3:
pr.append(i)
if i[0]<=3 and i[1]>3:
ct.append(i)
六项中有五项排序正确(一项缺失)。然而,最后我真正需要的是项目列表(变量名)以及它们所属的类别。
我已经看到,试图根据变量的定义重命名变量会变得丑陋。我一直在寻找不同的语法来打印结果,但我不知道我不知道的是什么。我考虑复制数组项并将其转换为字符串,然后打印相应的项标题。我觉得我把这件事搞复杂了
有人能建议一种更简单的方法来实现我的目标吗?在Python中,变量是引用对象的方法,而不是对象的名称。从一个对象到一个变量并不容易。此外,在处理多个对象时,更自然的做法是将它们放在列表或字典中,而不是将它们指定给变量 我认为这是同样的分类:
In [95]: SOC = np.array([1, 2, 2, 3, 3, 4])
...: SOR = np.array([6, 2, 3, 1, 2, 1])
In [96]: items = list(zip(SOC,SOR))
In [97]: items
Out[97]: [(1, 6), (2, 2), (2, 3), (3, 1), (3, 2), (4, 1)]
In [98]: result = [[] for _ in range(4)]
...: for n,i in enumerate(items):
...: if i[0]>3 and i[1]>3:
...: result[0].append((n,i))
...: if i[0]>3 and i[1]<=3:
...: result[1].append((n,i))
...: if i[0]<=3 and i[1]<=3:
...: result[2].append((n,i))
...: if i[0]<=3 and i[1]>3:
...: result[3].append((n,i))
...:
In [99]: result
Out[99]:
[[],
[(5, (4, 1))],
[(1, (2, 2)), (2, (2, 3)), (3, (3, 1)), (4, (3, 2))],
[(0, (1, 6))]]
或者直接在第一个循环中:
In [102]: result = [[] for _ in range(4)]
...: for n,i in enumerate(items):
...: if i[0]>3 and i[1]>3:
...: result[0].append('ABCDEF'[n])
...: if i[0]>3 and i[1]<=3:
...: result[1].append('ABCDEF'[n])
...: if i[0]<=3 and i[1]<=3:
...: result[2].append('ABCDEF'[n])
...: if i[0]<=3 and i[1]>3:
...: result[3].append('ABCDEF'[n])
...:
...:
In [103]: result
Out[103]: [[], ['F'], ['B', 'C', 'D', 'E'], ['A']]
[102]中的:范围内的[4]的结果=[]
…:对于n,枚举中的i(项):
…:如果i[0]>3且i[1]>3:
…:结果[0]。追加('ABCDEF'[n])
…:如果i[0]>3和i[1],您可以添加您想要得到的输出的样子吗?我觉得和OrderedDict
可以帮助您,但我想确保我了解您想要的是什么im=list(zip(SOC,SOR))更好地创建您的ITEM变量,特别是如果SOC/SOR是longOne,因为ITEM[0:5]不包含ITEM[5],所以缺少一个元素。在enumerate(ITEM):
中用迭代I,v。这就提供了要测试的元组和它在ITEM中的索引。我正在使用一个分析来根据矩阵中的关系对不同的项进行分类。你的回答帮助很大。看起来为了进一步的实现,我需要学习如何使用列表和字典谢谢你的澄清,你已经为我解释好了语法!
In [95]: SOC = np.array([1, 2, 2, 3, 3, 4])
...: SOR = np.array([6, 2, 3, 1, 2, 1])
In [96]: items = list(zip(SOC,SOR))
In [97]: items
Out[97]: [(1, 6), (2, 2), (2, 3), (3, 1), (3, 2), (4, 1)]
In [98]: result = [[] for _ in range(4)]
...: for n,i in enumerate(items):
...: if i[0]>3 and i[1]>3:
...: result[0].append((n,i))
...: if i[0]>3 and i[1]<=3:
...: result[1].append((n,i))
...: if i[0]<=3 and i[1]<=3:
...: result[2].append((n,i))
...: if i[0]<=3 and i[1]>3:
...: result[3].append((n,i))
...:
In [99]: result
Out[99]:
[[],
[(5, (4, 1))],
[(1, (2, 2)), (2, (2, 3)), (3, (3, 1)), (4, (3, 2))],
[(0, (1, 6))]]
In [100]: np.array(list('ABCDEF'))
Out[100]: array(['A', 'B', 'C', 'D', 'E', 'F'], dtype='<U1')
In [101]: [[np.array(list('ABCDEF'))[x[0]] for x in y] for y in result]
Out[101]: [[], ['F'], ['B', 'C', 'D', 'E'], ['A']]
In [102]: result = [[] for _ in range(4)]
...: for n,i in enumerate(items):
...: if i[0]>3 and i[1]>3:
...: result[0].append('ABCDEF'[n])
...: if i[0]>3 and i[1]<=3:
...: result[1].append('ABCDEF'[n])
...: if i[0]<=3 and i[1]<=3:
...: result[2].append('ABCDEF'[n])
...: if i[0]<=3 and i[1]>3:
...: result[3].append('ABCDEF'[n])
...:
...:
In [103]: result
Out[103]: [[], ['F'], ['B', 'C', 'D', 'E'], ['A']]