[英]How to multiply individual elements of numpy array of row ith with element of another numpy array of row ith?
[英]Python: How can I return the same array, where the ith element is removed in each row?
假設我有一個列表列表:
[[1,2,3,4,5], [11,22,33,44,55], [111,222,333,444,555]]
我怎樣才能簡單地返回相同的列表,給定任何i,其中每行的ith元素都將被刪除?
例如,如果i = 2,我們得到:
[[1,2,4,5], [11,22,44,55], [111,222,444,555]]
。
我試過了:
切片,但遇到了麻煩,例如list[0:i]
在i = 0時失敗。
使用my_list.index(i)
獲取索引值,但這失敗了,因為該函數要匹配字符串。
提前致謝。
您可以del
第i個項目。
i = 2
for x in my_list:
del x[i]
...
return my_list # Same list reference
輸出:
[[1, 2, 4, 5], [11, 22, 44, 55], [111, 222, 444, 555]]
據我了解,您不需要修改原始列表,並且可以滿意地使用原始列表的副本 ,其中彈出了每個內部列表的第i個元素。 列表理解和求助!
def pop_ith(lst, i):
return [x[0:i] + x[i+1:] for x in lst]
>>> a = [[1,2,3,4,5], [11,22,33,44,55], [111,222,333,444,555]]
>>> pop_ith(a,1)
[[1, 3, 4, 5], [11, 33, 44, 55], [111, 333, 444, 555]]
>>> pop_ith(a,0)
[[2, 3, 4, 5], [22, 33, 44, 55], [222, 333, 444, 555]]
>>> pop_ith(a,4)
[[1, 2, 3, 4], [11, 22, 33, 44], [111, 222, 333, 444]]
>>> pop_ith(a,6)
[[1, 2, 3, 4, 5], [11, 22, 33, 44, 55], [111, 222, 333, 444, 555]]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.