根据您的“它应该如何工作”,这是您正在寻找的内容。我使用了具有浮动值的示例数组,以便能够向您显示每行中的位置以及脚本末尾的值的排序,否则如果我使用整数是不可能的。
import numpy as np
# test data
solution = np.array([[1.1, 2.3, 3.2], [2.1, 1.2, 3.3], [1.3, 3.1, 2.2]], dtype = float)
best_solutions = np.zeros((3,3), dtype = float)
print solution
print best_solutions
shape_solutions = solution.shape
shape_best_solutions = best_solutions.shape
run, depth = shape_solutions
low = 1.2
upper = 1.4
positions_1 = 0
positions_2 = 0
positions_3 = 0
for i in range(1, run+1):
print 'check row : %s' % i
for j in range(1, depth+1):
value = solution[i-1][j-1]
print 'row/depth : %s-%s - Got value "%s".' % (i,j, value)
if value in [1.1, 1.2, 1.3]:
print 'This is value : %s' % value
best_solutions[0][positions_1] = value
positions_1 += 1
# this code is just here to show low and upper sorting creteria for your lower = 300 and upper = 500
if low < value < upper:
print "I've got a value ""%s"" between two limits ""%s-%s""..yeah!" % (value, low, upper)
elif value in [2.1, 2.2, 2.3]:
print 'This is value : %s' % value
best_solutions[1][positions_2] = value
positions_2 += 1
elif value in [3.1, 3.2, 3.3]:
print 'This is value : %s' % value
best_solutions[2][positions_3] = value
positions_3 += 1
print best_solutions
best_row, best_depth = shape_best_solutions
#sort values in each row/run
for i in range(best_row):
best_solutions[i].sort()
print best_solutions
从我这边来看,标准输出如下所示:
[[1.1 2.3 3.2]
[2.1 1.2 3.3]
[1.3 3.1 2.2]]
[[0. 0. 0.]
[0. 0. 0.]
[0. 0. 0.]]
check row : 1
row/depth : 1-1 - Got value "1.1".
This is value : 1.1
row/depth : 1-2 - Got value "2.3".
This is value : 2.3
row/depth : 1-3 - Got value "3.2".
This is value : 3.2
check row : 2
row/depth : 2-1 - Got value "2.1".
This is value : 2.1
row/depth : 2-2 - Got value "1.2".
This is value : 1.2
row/depth : 2-3 - Got value "3.3".
This is value : 3.3
check row : 3
row/depth : 3-1 - Got value "1.3".
This is value : 1.3
I've got a value 1.3 between two limits 1.2-1.4..yeah!
row/depth : 3-2 - Got value "3.1".
This is value : 3.1
row/depth : 3-3 - Got value "2.2".
This is value : 2.2
[[1.1 1.2 1.3]
[2.3 2.1 2.2]
[3.2 3.3 3.1]]
[[1.1 1.2 1.3]
[2.1 2.2 2.3]
[3.1 3.2 3.3]]
你想要这个吗?
[[[1.1],
[ 1.2],
[ 1.3]],
[[2.1],
[2.2],
[2.3]],
[[3.1],
[3.2],
[3.3]]]
灵活性:
import numpy as np
# test data
solution = np.array([[1.1, 2.3, 3.2, 4.1], [2.1, 1.2, 3.3, 4.2], [1.3, 3.1, 2.2, 2.4]], dtype = float)
#solution = np.array([[[1.1, 4.1], [2.3, 4.2], [3.2, 4.3]], [[2.1, 4.4], [1.2, 4.5], [3.3, 4.6]], [[1.3, 4.7],[3.1, 4.8], [2.2, 4.9]]], dtype = float)
sort_filters = ((1.0, 1.4), (2.0, 2.5), (3.0, 3.4), (4.0, 4.4))
# all values might be within range of one filter set. Better to set column length to max.
column_width = solution.size
solutions_shape = solution.shape
print 'solutions_shape : %s' % str(solutions_shape)
print 'best solutions column_width : %s' % (column_width)
# Select bast solutions array based on solutions array shape.
array_type = 0
if len(solutions_shape) == 3: # selects if it has 3rd dimension.
print 'got 3d array', solution
rows, subrows, columns = solutions_shape
array_type = 3
# the number of rows are based on filter size
best_solutions = np.zeros((len(sort_filters), subrows, columns), dtype = float)
else:
print 'got 2d array\n', solution
rows, columns = solutions_shape
array_type = 2
# the number of rows are based on number of filter sets.
best_solutions = np.zeros((len(sort_filters), column_width), dtype = float)
print best_solutions
shape_best_solutions = best_solutions.shape
# location for best_solutions where next value should be stored.
next_position = {}
if array_type == 2:
for i in range(1, rows+1):
# print 'check row : %s' % i
for j in range(1, columns+1):
value = solution[i-1][j-1]
# print '\nrow/columns : %s-%s - Got value "%s".' % (i,j, value)
filter_number = 0
for selection_keys in sort_filters:
x, y = selection_keys
# print ' filter_no.... : %s - values : (%s, %s)' % (filter_number, x, y)
row_position = filter_number
# print ' next_position : %s\n' % (next_position)
# create row entry for position storage based on sort_filter sets
if next_position.has_key(row_position) != True:
next_position[row_position] = 0
# print ' entry no. added : %s' % row_position
if x < value < y:
#print 'This is value : %s' % value
column_position = next_position[row_position]
# print ' best sol.col.no : %s.' % column_position
best_solutions[row_position][column_position]= value
next_position[row_position] = column_position + 1
# print best_solutions
else:
# print "no fit"
pass
filter_number += 1
print best_solutions, next_position
# removes excess columns with zeros
last_column_with_values = next_position.values()
last_column_with_values.sort()
remove_column_from = last_column_with_values.pop()
best_row, best_columns = shape_best_solutions
best_solutions = np.delete(best_solutions, np.s_[remove_column_from:best_columns], axis=1)
print best_solutions
#sort values in each row/run
for i in range(best_row):
best_solutions[i].sort()
print best_solutions
应该打印出来:
solutions_shape : (3L, 4L)
best solutions column_width : 12
got 2d array
[[1.1 2.3 3.2 4.1]
[2.1 1.2 3.3 4.2]
[1.3 3.1 2.2 2.4]]
[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
[[1.1 1.2 1.3 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[2.3 2.1 2.2 2.4 0. 0. 0. 0. 0. 0. 0. 0. ]
[3.2 3.3 3.1 0. 0. 0. 0. 0. 0. 0. 0. 0. ]
[4.1 4.2 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. ]] {0: 3, 1: 4, 2: 3, 3: 2}
[[1.1 1.2 1.3 0. ]
[2.3 2.1 2.2 2.4]
[3.2 3.3 3.1 0. ]
[4.1 4.2 0. 0. ]]
[[0. 1.1 1.2 1.3]
[2.1 2.2 2.3 2.4]
[0. 3.1 3.2 3.3]
[0. 0. 4.1 4.2]]