python多维列表索引,Python:在给定索引列表的情况下访问多维列表的元素

I have a multidimensional list F, holding elements of some type. So, if for example the rank is 4, then the elements of F can be accessed by something like F[a][b][c][d].

Given a list L=[a,b,c,d], I would like to access F[a][b][c][d]. My problem is that my rank is going to be changing, so I cannot just have F[L[0]][L[1]][L[2]][L[3]].

Ideally, I would like to be able to do F[L] and get the element F[a][b][c][d]. I think something like this can be done with numpy, but for the types of arrays that I'm using, numpy is not suitable, so I want to do it with python lists.

How can I have something like the above?

EDIT: For a specific example of what I'm trying to achieve, see the demo in Martijn's answer.

解决方案

You can use the reduce() function to access consecutive elements:

from functools import reduce # forward compatibility

import operator

reduce(operator.getitem, indices, somelist)

In Python 3 reduce was moved to the functools module, but in Python 2.6 and up you can always access it in that location.

The above uses the operator.getitem() function to apply each index to the previous result (starting at somelist).

Demo:

>>> import operator

>>> somelist = ['index0', ['index10', 'index11', ['index120', 'index121', ['index1220']]]]

>>> indices = [1, 2, 2, 0]

>>> reduce(operator.getitem, indices, somelist)

'index1220'

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在 Python 中,可以使用 del 关键字来删除多维数组中的一组元素。语法如下: ``` del array_name[index1][index2]...[indexn] ``` 其中,index1、index2、...、indexn 是需要删除元素所在的多维索引。 例如,如果有一个二维数组 a,要删除其中第二行第三列的元素,可以使用如下语句: ``` del a[1][2] ``` 注意:删除数组中的元素将导致数组的大小减小,并且已删除元素的位置上将不再有值。 ### 回答2: Python中删除多维数组中一组元素可以使用以下方法: 1. 使用列表推导式:可以通过列表推导式来生成一个新的多维数组,不包含需要删除的元素组。例如,假设有一个含有3个子列表多维数组arr,要删除索引为1和2的子列表,可以使用以下代码: ``` arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] new_arr = [sub for idx, sub in enumerate(arr) if idx not in [1, 2]] ``` 在这个例子中,使用了enumerate函数来获取子列表索引,如果索引不在给定的要删除的索引列表[1, 2]中,则将该子列表添加到新的多维数组new_arr中。 2. 使用numpy库:如果需要处理大型多维数组,可以使用numpy库的功能来删除一组元素。首先,需要导入numpy库,并创建一个numpy数组。然后,使用numpy库提供的删除函数删除指定的子数组,例如,假设要删除多维数组中索引为1和2的子数组,可以使用以下代码: ``` import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]) new_arr = np.delete(arr, [1, 2], axis=0) ``` 在这个例子中,使用了np.delete函数来删除指定索引的子数组,参数axis=0表示按行删除。 这些方法都是将需要删除的元素组筛选出来,并创建新的数组。原始数组并没有被修改。 ### 回答3: 在Python中,可以使用列表推导式来删除多维数组中的一组元素。 假设我们有一个多维数组arr,我们想要删除其中的一组元素group。 首先,我们可以使用列表推导式生成一个新的多维数组,其中不包含需要删除的元素组。 代码示例: ```python arr = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] group = [4, 5, 6] new_arr = [x for x in arr if x != group] ``` 在上面的示例中,我们通过遍历原始多维数组arr中的每个元素x,并检查x是否与group相等。如果不相等,则将x添加到新的多维数组new_arr中。 最终,new_arr将不包含被删除的元素组[4, 5, 6]。 需要注意的是,这种方法只能删除一次出现的元素组。如果要删除多个出现的元素组,可以使用循环和条件语句来实现。 值得一提的是,使用这种方式生成的新数组内部的子数组仍然是原始数组的引用,也就是说如果修改new_arr中的某个子数组,原始数组arr中对应的子数组也会被修改。如果希望得到完全独立的新数组,可以使用深拷贝来生成。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值