我想在Python中编写这个C#函数,不幸的是我在IndexError: list assignment index out of range
行中有 ERROR :Population.BinaryX.insert([i][j], dec)
。任何人都可以告诉我如何解决这个问题?!我做错了什么?
C#CODE:
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)
答案 0 :(得分:3)
考虑[i][j]
中的Population.BinaryX.insert([i][j], dec)
表达式。该表达式创建一个包含i
值的1项列表,然后尝试从该列表中获取j
项。
>>> i=1
>>> j=2
>>> [i][j]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: list index out of range
Python列表是一维的,如果您需要多维列表,则需要创建列表列表或使用其他结构,例如numpy
或pandas
数组。
一个选项是使用已知值预分配数组,以便稍后可以将其编入索引
@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
答案 1 :(得分:1)
请确保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] 在您发表意见并尝试理解您的代码之后。这就是我所拥有的: -
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
和print语句中有一些起始值供您查看输出。
生成输出:
[[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]