【问题标题】:IndexError in array PYTHON数组 PYTHON 中的 IndexError
【发布时间】:2016-05-09 22:06:27
【问题描述】:

我想用 Python 编写这个 C# 函数,不幸的是我有这个 ERROR: IndexError: list assignment index out of range in Population.BinaryX.insert([i][j], dec) 行。谁能告诉我如何解决这个问题?!我做错了什么?

C# 代码:

public class Population
    {
        public float[] CodedX = new float[20];
        public float[,] BinaryX = new float[10, 8];
    }

private void BinaryTranslating()
        {
            int dec;
            int j = 0;
            for (var i = 0; i < 10; i++)
            {
                while (Population.CodedX[i] > 1 & j < 8)
                {
                    dec = (int)Population.CodedX[i] % 2;
                    Population.BinaryX[i, j] = dec;
                    Population.CodedX[i] /= 2;
                    j++;
                }
                j = 0;
            }
        }

 private void DecimalTranslating()
        {
            for (var i = 0; i < 10; i++)
            {
                Population.CodedX[i] = Population.BinaryX[i, 7] * 128 + Population.BinaryX[i, 6] * 64 + 
                                       Population.BinaryX[i, 5] * 32 + Population.BinaryX[i, 4] * 16 + 
                                       Population.BinaryX[i, 3] * 8 + Population.BinaryX[i, 2] * 4 + 
                                       Population.BinaryX[i, 1] * 2 + Population.BinaryX[i, 0];

            }
        }

Python 代码:

class Population:
CodedX = []
BinaryX = [[], []]

class Application:
@staticmethod
    def binary_translating():
        j = 0
        for i in range(10):
            while Population.CodedX[i] > 1 & j < 8:
                dec = int(Population.CodedX[i]) % 2
                Population.BinaryX.insert([i][j], dec)
                Population.CodedX[i] /= 2
                j += 1
            j = 0

@staticmethod
    def decimal_translating():
        for i in range(10):
            new_item = Population.BinaryX[i][7] * 128 + Population.BinaryX[i][6] * 64 + Population.BinaryX[i][5] * 32 +\
                       Population.BinaryX[i][4] * 16 + Population.BinaryX[i][3] * 8 + Population.BinaryX[i][2] * 4 +\
                       Population.BinaryX[i][1] * 2 + Population.BinaryX[i][0]
            Population.CodedX.insert(i, new_item)

【问题讨论】:

  • 不应该对布尔值使用按位与,在 Python 中使用正确的“与”运算符。
  • 你能告诉我们你是如何定义Population.BinaryX的吗?
  • 您可以发布 CodedX 的示例吗?什么是 BinaryX?
  • [i][j] 没有做你想做的事。
  • BinaryX 一个多维列表/数组吗?

标签: c# python arrays function python-3.x


【解决方案1】:

考虑Population.BinaryX.insert([i][j], dec) 中的[i][j] 表达式。该表达式创建一个包含 i 值的 1 项列表,然后尝试从该列表中获取 jth 项。

>>> i=1
>>> j=2
>>> [i][j]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list index out of range

Python 列表是一维的,如果您想要一个多维列表,您需要创建一个列表列表或使用一些其他结构,例如 numpypandas 数组。

一种选择是使用已知值预先分配数组,以便以后可以简单地对其进行索引

@staticmethod
def binary_translating():
    Population.BinaryX = [[None] * 8 for _ in range(10)]
    j = 0
    for i in range(10):
        while Population.CodedX[i] > 1 & j < 8:
            dec = int(Population.CodedX[i]) % 2
            Population.BinaryX[i][j] = dec
            Population.CodedX[i] /= 2
            j += 1
        j = 0

另一种选择是插入到子列表中:

@staticmethod
def binary_translating():
    Population.BinaryX = []
    j = 0
    for i in range(10):
        Population.BinaryX.insert([])
        while Population.CodedX[i] > 1 & j < 8:
            dec = int(Population.CodedX[i]) % 2
            Population.BinaryX[i].insert(j, dec)
            Population.CodedX[i] /= 2
            j += 1
        j = 0

【讨论】:

  • 我正在尝试使用您的第一个版本,我有疑问我们可以这样写吗Population.BinaryX[i][j] = dec 它给了IndexError: list assignment index out of range 你对此有何看法?
  • 你能再看看我的帖子吗?我用与上一个函数相关的新函数更新了它,也就是我定义Population.BinaryX的地方。
  • @NurzhanNogerbek - 我离开我的电脑有一段时间了......很高兴看到其他答案为你解决了问题!
【解决方案2】:

请确保Population.BinaryX 是一个有效实体并且其中至少有10 个元素,因为您正在运行循环10 次。 CodedX 也是如此。

如果其中任何一个都没有至少 10 个元素,你会得到

IndexError: 列表赋值索引超出范围

试试..

class Population:
    CodedX = [0 for i in range(10)]
    BinaryX = [[0 for i in range(10)] for j in range(10)]

这是 tdelaney 提到的预分配。

如果您查看 Application 类中的每个函数,它们会尝试使用 BinaryX 或 CodedX 数组,但如果这些数组中没有任何元素,那么 python 将如何索引它们?

我的意思是在调用decimal_translating() 函数之前,Population.BinrayX 中必须有元素。不能为空数组。

同样,在调用binary_translating() 函数之前,Population.CodedX 中必须有元素。

[编辑 #1] 在您的 cmets 并试图理解您的代码之后。这是我所拥有的:-

class Population(object):
    def __init__(self):
        self.CodedX = [0 for i in range(10)]  # as per your C# code
        self.BinaryX = []
        # debug code to fill CodedX array - remove it
        for i in range(10):
            self.CodedX[i] = int(761)

    def binary_translating(self):
        for i in range(10):
            j = 0
            self.BinaryX.append([0 for k in range(10)])
            while (self.CodedX[i] > 0) and (j < 10):
                dec = int(self.CodedX[i] % 2)
                self.BinaryX[i][j] = dec  # this will append at j
                self.CodedX[i] = int(self.CodedX[i] / 2)
                j += 1
        # debug code to print - remove it
        print(self.BinaryX)
        # debug code to clear CodedX - remove it
        for i in range(10):
            self.CodedX[i] = int(0)

    def decimal_translating(self):
        for i in range(10):
            value = self.BinaryX[i][7] * 128 + self.BinaryX[i][6] * 64 + self.BinaryX[i][5] * 32 + \
                             self.BinaryX[i][4] * 16 + self.BinaryX[i][3] * 8 + self.BinaryX[i][2] * 4 + \
                             self.BinaryX[i][1] * 2 + self.BinaryX[i][0]
            self.CodedX[i] = value
        print(self.CodedX)


pop = Population()

pop.binary_translating()
pop.decimal_translating()

我添加了一些调试代码,以便在 CodedX 中有一些起始值并打印语句供您查看输出。

生成输出:

[[1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1], [1, 0, 0, 1, 1, 1, 1, 1, 0, 1]]

[249, 249, 249, 249, 249, 249, 249, 249, 249, 249]

[编辑#2]

【讨论】:

  • 你好 @Mrunmoy 我现在检查了程序,我在 Population.CodedX 中有元素。我还注意到有趣的想法,当我写 BinaryX = [[0 for i in range(10)] for j in range(10)] 时,它起作用了,但是当我写为 BinaryX = [[0 for i in range(8)] for j in range(10)] 时,它不起作用。所以我对此有点困惑。你有什么想法吗?!
  • 顺便说一下,Population.BinrayX 我第一次在binary_translating() 函数中使用,所以它在开始时无论如何都是空的。在那个原因中该怎么办?
  • 据我了解,我需要将元素添加到Population.BinrayX,但是如何使用 .append 到多维数组中呢?!
  • 你的意思是Population.BinaryX从零开始填充?还是应该是空的?
  • 我要求将 10 更改为 8,因为这会改变转换的精度。通过将 10 更改为 8,您将转换为 8 位二进制而不是 10 位二进制,因此您可以转换的值将被限制为 8 位值。
猜你喜欢
  • 2020-08-12
  • 1970-01-01
  • 2019-09-04
  • 1970-01-01
  • 1970-01-01
  • 2020-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多