random_pool=[[[0, 2, 3, 1, 3, 2, 0, 1], [0, 1], [1], [0]],
[[0, 3, 2, 1, 2, 3, 0, 1], [0, 3], [3], [1]],
[[1, 2, 3, 0, 3, 2, 1, 0], [2, 2], [4], [2]],
[[2, 1, 3, 0, 3, 1, 2, 0], [2, 1], [3], [3]]]
def BinaryConversion(random_pool):
global binary_list
binary_list = []
binary_index = {0: (0,0), 1: (0,1), 2: (1,0), 3: (1,1)}
for game in range (0, len(random_pool)):
converted = [s for num in random_pool[game][0] for s in binary_index[num]]
binary_list.append(converted)
return (binary_list)
def GeneratePopulation(random_pool, binary_list):
global population
individual = []
population = []
for ind in range (0, len(binary_list)):
individual = [binary_list[ind],random_pool[ind][2],random_pool[ind][3]]
population.append(individual)
return (population)
def BinaryReturn(population):
binary_return_l=[]
binary_return_index = {(0,0): 0, (0,1): 1, (1,0): 2, (1,1): 3}
for game in range (0, len(population)):
returned = [s for num in population[game][0] for s in binary_return_index[num]]
binary_return_l.append(returned)
return(bynary_return_l)
binary = BinaryConversion(random_pool)
print ("\n")
print ("\n",binary_list)
pop = GeneratePopulation(random_pool, binary_list)
print ("\n")
print ("\n",population)
unconverted = BinaryReturn(population)
print ("\n")
print ("\n",bynary_return_l)
大家好,我有上面的代码来执行以下操作,1-根据字典引用将整数向量转换为二进制(工作正常),2-生成一个“填充”来附加另一个列表中的项(我认为这在这里并不重要),和3-根据相同的逻辑将二进制字符串转换回数字
问题在第三部分,BinaryReturn(population)
。我试图从二进制转换代码执行逆操作,但它不起作用,我得到消息KeyError: 0
。有人知道我做错了什么吗
谢谢你
目前没有回答
相关问题 更多 >
编程相关推荐