python打印输出不一致

时间:2017-09-27 12:54:23

标签: python arrays sorting numpy multidimensional-array

(Python 2.7.12) - 我创建了一个NxN数组,当我打印它时,我得到了以下完整的输出:

示例

SampleArray=sorted(SampleArray, key=lambda x: x[4])
  • 很干净。

但是,当我使用代码

按第4列中的元素对此数组进行排序时
[array([90,  9, 77, 63, 48]), array([43, 97, 47, 74, 53]), array([60, 64, 97,  2, 73]), array([34, 20, 42, 80, 76]), array([86, 61, 95, 21, 82])]

我得到以下输出:

示例b:

SELECT cycle
FROM product AS pr
INNER JOIN productdetails AS prd ON pr.cycle = prd.cycle 
WHERE prd.label = 'XRT354511' 

UNION ALL

SELECT cycle
FROM product AS pr
INNER JOIN productdetails AS prd 
ON  SUBSTRING(prd.cycle, 1, LEN(pr.cycle)+9) COLLATE LATIN1_GENERAL_BIN = (pr.cycle + '-EXECUTED') COLLATE LATIN1_GENERAL_BIN
WHERE prd.label = 'XRT354511' 
AND pr.cycle <> prd.cycle 

如何让我的输出保持为&#39; Sample a&#39;的格式。如果我能在直列中看到数字,它将使调试变得更容易。

3 个答案:

答案 0 :(得分:3)

只需numpy.argsort()例程:

import numpy as np

a = np.random.randint(1,100, size=(5,5))
print(a)   # initial array
print(a[np.argsort(a[:, -1])])  # sorted array

# initial array的输出:

[[21 99 34 33 55]
 [14 81 92 44 97]
 [68 53 35 46 22]
 [64 33 52 40 75]
 [65 35 35 78 43]]

# sorted array的输出:

[[68 53 35 46 22]
 [65 35 35 78 43]
 [21 99 34 33 55]
 [64 33 52 40 75]
 [14 81 92 44 97]]

答案 1 :(得分:0)

你只需要使用

将样本数组转换回numpy数组
SampleArray = np.array(SampleArray)

示例代码: -

import numpy as np
SampleArray=np.random.randint(1,100, size=(5,5))    

print (SampleArray)
SampleArray=sorted(SampleArray, key=lambda x: x[4])
print (SampleArray)
SampleArray = np.array(SampleArray)
print (SampleArray)

输出: -

[[28 25 33 56 54]
 [77 88 10 68 61]
 [30 83 77 87 82]
 [83 93 70  1  2]
 [27 70 76 28 80]]
[array([83, 93, 70,  1,  2]), array([28, 25, 33, 56, 54]), array([77, 88, 10, 68, 61]), array([27, 70, 76, 28, 80]), array([30, 83, 77, 87, 82])]
[[83 93 70  1  2]
 [28 25 33 56 54]
 [77 88 10 68 61]
 [27 70 76 28 80]
 [30 83 77 87 82]]

答案 2 :(得分:-1)

这可以提供帮助:

from pprint import pprint
pprint(SampleArray)

输出与Sample A略有不同,但它仍然看起来很整洁,调试也会更容易。

编辑:这是我的输出

[[92  8 41 64 61]
 [18 67 91 80 35]
 [68 37  4  6 43]
 [26 81 57 26 52]
 [ 6 82 95 15 69]]

[array([18, 67, 91, 80, 35]),
 array([68, 37,  4,  6, 43]),
 array([26, 81, 57, 26, 52]),
 array([92,  8, 41, 64, 61]),
 array([ 6, 82, 95, 15, 69])]