English 中文(简体)
• 如何把2D-030 Array混为一谈? a. “TypeError:清单指标必须是分类,而不是图形”
原标题:How to slice a 2D Python Array? Fails with: "TypeError: list indices must be integers, not tuple"
  • 时间:2010-09-09 20:20:26
  •  标签:
  • python
  • numpy

我在一流模块中有2个阵列,如:

data = array([[1,2,3],
              [4,5,6],
              [7,8,9]])

我想得到这种阵列的斜体,只包括某些元件。 例如,我可能希望第0栏和第2栏:

data = [[1,3],
        [4,6],
        [7,9]]

这样做的最残酷方式是什么? (无 lo)

我认为,这将发挥作用:

newArray = data[:,[0,2]]

但结果如下:

TypeError: list indices must be integers, not tuple
问题回答

错误明确说:数据不是一阵列,而是一份清单。

努力将其转换成一阵列:

numpy.array(data)[:,[0,2]]

如果您想引用2Dlist的话,以下职能可能有助于:

def get_2d_list_slice(self, matrix, start_row, end_row, start_col, end_col):
    return [row[start_col:end_col] for row in matrix[start_row:end_row]]

实际上,你所写的作品应当只做罚款...... 你们使用哪种 n?

仅作核实,以下工作就应当与最新版本的假设完全一致:

import numpy as np
x = np.arange(9).reshape((3,3)) + 1
print x[:,[0,2]]

对我来说,哪些是:

array([[1, 3],
       [4, 6],
       [7, 9]])

......

THis may not be what you are looking for but this is would do. zip(*x)[whatever columns you might need]

www.un.org/Depts/DGACM/index_spanish.htm 为什么在Numpy,但不是在Alpha/strong,

因为__getitem__ 您可以安排班子,用<条码>:和多种论点做你想要做的事情。

假设情况确实如此,但已建在<代码>>><>/code>上。

更确切地说:

class C(object):
    def __getitem__(self, k):
        return k

# Single argument is passed directly.
assert C()[0] == 0

# Multiple indices generate a tuple.
assert C()[0, 1] == (0, 1)

# Slice notation generates a slice object.
assert C()[1:2:3] == slice(1, 2, 3)

# If you omit any part of the slice notation, it becomes None.
assert C()[:] == slice(None, None, None)
assert C()[::] == slice(None, None, None)
assert C()[1::] == slice(1, None, None)
assert C()[:2:] == slice(None, 2, None)
assert C()[::3] == slice(None, None, 3)

# Tuple with a slice object:
assert C()[:, 1] == (slice(None, None, None), 1)

# Ellipsis class object.
assert C()[...] == Ellipsis

那么,我们就可以把物体打开:

s = slice(1, 2, 3)
assert s.start == 1
assert s.stop == 2
assert s.step == 3

因此,你写了:

[][1, 2]

Python:

TypeError: list indices must be integers, not tuple

由于你试图通过<代码>(1、2)至__getitem__,而已编清单则没有编入方案处理图形论点,只是分类。

Beware that numpy only accept regular array with the same size for each elements. you can somehow use : [a[i][0:2] for i in xrange(len(a))] it s pretty ugly but it works.

newArray = data[:,0:2]

还是我失踪了吗?

该例子始于array,而不是np.array-array,但并未被界定为一种孤立的序号:

data = array([[1,2,3],
              [4,5,6],
              [7,8,9]])

data[:,[0,2]]

错误:

Name错误: name  array  is not defined

为了复制这一错误,您必须放弃array (不含np. in front, 它没有任何定义。

data = [[1,2,3],
        [4,5,6],
        [7,8,9]]

data[:,[0,2]]

错误:

Type错误: list indices must be integers or slices, not tuple

用户可能利用阵列的内部清单进行测试,但用<代码>np.array输出的复印件询问这一问题。 至少在2021年,这个问题是完全错误的:不能照搬。 我怀疑这种行为在2010年是不同的(假设是<>>> > /em>基本 package子。

完整性,如其他答复:

data = np.array([[1,2,3],
                [4,5,6],
                [7,8,9]])

data[:,[0,2]]

产出:

array([[1, 3],
       [4, 6],
       [7, 9]])

你们不需要一份密封的清单来照搬。 以两个层面,如一维名单

[1,2][:, 0]

throws the same Type错误: list indices must be integers or slices, not tuple.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...