我想檢查陣列中的條件以及條件是否為真。我想列印陣列的行。如果條件為假。我想列印陣列中的另一組行。例子
A1 = np.array(
[[ 54. 15. 1. 0. ]
[ 55. 13.66311896 1. 1.33688104]
[ 56. 10.16311896 1.95491503 4.93024146]
[ 57. 5.83688104 4.45491503 9.79281302]
[ 58. 2.33688104 7.54508497 14.25456836]
[ 59. 1. 10.04508497 16.66774016]
[ 60. 1. 11. 17.20465053]
[ 61. 1.47745751 11. 16.81841715]
[ 62. 2.72745751 10.04508497 15.24561777]
[ 63. 4.27254249 7.54508497 12.56648248]
[ 64. 5.52254249 4.45491503 10.0875487 ]
[ 80. 4.92705098 2.33688104 10.16127712]
[ 81. 3.07294902 5.83688104 12.8705075 ]
[ 82. 1.57294902 10.16311896 16.25572045]
[ 83. 1. 13.66311896 18.87735632]
[ 84. 1. 15. 19.79898987]]
現在,我想檢查第 3 列中元素的值是否比前一個元素增加(即,如果在第 3 列中 i 1 > i),那么所有增加的元素資料應分組在 1 個陣列中。如果條件為假。其余部分必須分組到另一個陣列中。
這意味著條件:在 A1 0 > 1.33688104 > 4.93024146 > 9.79281302 > 14.25456836 > 16.66774016 > 17.20465053 > 18.87735632 > 19.79898987 的第三列中,因此輸出應該是
Array1 = [ 54. 15. 1. 0. ]
[ 55. 13.66311896 1. 1.33688104]
[ 56. 10.16311896 1.95491503 4.93024146]
[ 57. 5.83688104 4.45491503 9.79281302]
[ 58. 2.33688104 7.54508497 14.25456836]
[ 59. 1. 10.04508497 16.66774016]
[ 60. 1. 11. 17.20465053]
[83. 1. 13.66311896 18.87735632]
[ 84. 1. 15. 19.79898987]
Array2:
[ 61. 1.47745751 11. 16.81841715]
[ 62. 2.72745751 10.04508497 15.24561777]
[ 63. 4.27254249 7.54508497 12.56648248]
[ 64. 5.52254249 4.45491503 10.0875487 ]
[ 80. 4.92705098 2.33688104 10.16127712]
[ 81. 3.07294902 5.83688104 12.8705075 ]
[ 82. 1.57294902 10.16311896 16.25572045]
我想重復這個回圈,直到陣列中保留一個值。
我的代碼
pre = 0
list1 = [] # Residual
list2 = [] # part of increasing 3rd row
for index in A1[:,3]:
a = index > pre
if a == False:
list1.append(index) # Increasing 3rd row
else:
list2.append(index) # other remainin values
pre = index
我試過這樣做,但我只能將最后一行列印為串列。請幫我在 python 的陣列中列印整行。
uj5u.com熱心網友回復:
看起來您可能誤解了 Python 回圈的引數。雖然您的回圈對于比較陣列值是正確的,但這種實作使得將所需的行附加到您希望創建的新串列變得更加困難。
即回圈A1[:,3]意味著您的“索引”值只是在第三列中找到的值,而如果您要回圈,A1您會發現“索引”變數現在是整行并且可以附加到您的最終串列中相對容易;但是,Python 還允許您使用該range()函式回圈一組整數,就像我在示例中所做的那樣。這允許 Python 的回圈表現得更接近于您在其他一些語言中所期望的。在我的代碼中,我使用了 range() 函式,但是您可以輕松地調整此代碼以回圈遍歷 A1 的行而不是索引值。
另一個小提示:您似乎使用 numpy 陣列作為輸入,但隨后在函式中使用串列。我通常會嘗試使用一種陣列,以便您可以在所有陣列之間可靠地使用相同的方法。在這種情況下,它不會太麻煩,但只是未來需要注意的事情。
這是我的代碼示例:
pre = A1[0][3]
list1 = [] # Increasing Row 3
list1.append(A1[0])
list2 = [] # Residual
for index in range(1,len(A1)):
if (A1[index][3] > pre):
list1.append(A1[index]) # Increasing 3rd row
pre = A1[index][3]
else:
list2.append(A1[index]) # Residual values
print("List 1 (Increasing Values):")
for row in list1:
print(row)
print("\nList 2 (Non-Increasing Values):")
for row in list2:
print(row)
您會看到我還調整了“pre”和“list1”變數的初始值,因此使用第一行作為這些值的起點。否則,當使用不同的陣列輸入(例如最初為負數)時,您可能會發現代碼有點僵硬。(同樣在這種情況下,您的第一行的值等于初始值的值)
uj5u.com熱心網友回復:
應該這樣做,檢查下面的輸出,看看它是否是你想要的:
def your_function(a, index=3):
# create the array to return: a list of slices,
# where the third column always contains ascending elements
array_output_slices = list()
# work on the remaining slice (initially set to the whole array)
reminder = a
iteration = 1
# until nothing remains (i.e. we processed the whole array)
while not reminder.size == 0:
index_column = reminder[:, index]
# get the index of the last element in ascending order
max_ordered_index = 0
while max_ordered_index 1 < len(index_column) and index_column[max_ordered_index 1] > index_column[max_ordered_index] :
max_ordered_index = 1
# get a slice and the remainder
output_slice = reminder[:max_ordered_index 1]
reminder = reminder[max_ordered_index 1:]
print(f"A{iteration}:\n", output_slice, "\n")
print(f"Reminder:\n", reminder, "\n\n--------------------------------------------------\n\n")
# append the slice to the list of slices to output
array_output_slices.append(output_slice)
iteration = 1
return array_output_slices
使用您的陣列作為輸入,它會列印:
A1:
[[54. 15. 1. 0. ]
[55. 13.66311896 1. 1.33688104]
[56. 10.16311896 1.95491503 4.93024146]
[57. 5.83688104 4.45491503 9.79281302]
[58. 2.33688104 7.54508497 14.25456836]
[59. 1. 10.04508497 16.66774016]
[60. 1. 11. 17.20465053]]
Reminder:
[[61. 1.47745751 11. 16.81841715]
[62. 2.72745751 10.04508497 15.24561777]
[63. 4.27254249 7.54508497 12.56648248]
[64. 5.52254249 4.45491503 10.0875487 ]
[80. 4.92705098 2.33688104 10.16127712]
[81. 3.07294902 5.83688104 12.8705075 ]
[82. 1.57294902 10.16311896 16.25572045]
[83. 1. 13.66311896 18.87735632]
[84. 1. 15. 19.79898987]]
--------------------------------------------------
A2:
[[61. 1.47745751 11. 16.81841715]]
Reminder:
[[62. 2.72745751 10.04508497 15.24561777]
[63. 4.27254249 7.54508497 12.56648248]
[64. 5.52254249 4.45491503 10.0875487 ]
[80. 4.92705098 2.33688104 10.16127712]
[81. 3.07294902 5.83688104 12.8705075 ]
[82. 1.57294902 10.16311896 16.25572045]
[83. 1. 13.66311896 18.87735632]
[84. 1. 15. 19.79898987]]
--------------------------------------------------
A3:
[[62. 2.72745751 10.04508497 15.24561777]]
Reminder:
[[63. 4.27254249 7.54508497 12.56648248]
[64. 5.52254249 4.45491503 10.0875487 ]
[80. 4.92705098 2.33688104 10.16127712]
[81. 3.07294902 5.83688104 12.8705075 ]
[82. 1.57294902 10.16311896 16.25572045]
[83. 1. 13.66311896 18.87735632]
[84. 1. 15. 19.79898987]]
--------------------------------------------------
A4:
[[63. 4.27254249 7.54508497 12.56648248]]
Reminder:
[[64. 5.52254249 4.45491503 10.0875487 ]
[80. 4.92705098 2.33688104 10.16127712]
[81. 3.07294902 5.83688104 12.8705075 ]
[82. 1.57294902 10.16311896 16.25572045]
[83. 1. 13.66311896 18.87735632]
[84. 1. 15. 19.79898987]]
--------------------------------------------------
A5:
[[64. 5.52254249 4.45491503 10.0875487 ]
[80. 4.92705098 2.33688104 10.16127712]
[81. 3.07294902 5.83688104 12.8705075 ]
[82. 1.57294902 10.16311896 16.25572045]
[83. 1. 13.66311896 18.87735632]
[84. 1. 15. 19.79898987]]
Reminder:
[]
--------------------------------------------------
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/420235.html
標籤:
上一篇:如何在跟蹤串列“編號”innR的同時將串列串列轉換為資料框?
下一篇:使用Python逐項迭代多個串列
