【发布时间】:2022-11-20 23:00:16
【问题描述】:
我有一个列表(df_pop_initial_list),它看起来像这样:
[['000000000000000000000000000001011000000'],
['000000001000000000000001000000000010000'],
['000000000000000000000000000000010011000'],
['000000000000001001000000000000010000000'],
['000000000000000000010000001000000010000'],
['1000000000100000000010000000000000000000'],
['1000000010000000000001000000000000000000'],
['1001000000000000000010000000000000000000'],
['000000000000100000000000100000000000010'],
['000000000110000000000000000000001000000'],
['000000101000000010000000000000000000000'],
['000000000000001000000010000100000000000'],
['000000000000000010000101000000000000000'],
['000000001000100000000000000000000100000'],
['000000100000000000000000010000001000000'],
['000000000000001100000000000010000000000'],
['010000000000000000000000000001001000000'],
['000000010100000001000000000000000000000'],
['000000000000000000001000000001100000000'],
['000100000000000100000000000000000000010']]
我正在尝试计算这个 39 bits 字符串列表中的 1,并将每个字符串值转换为 3 个整数,其中位为 on(表示 finding 1's)。
我的代码如下所示:
#Finding locations (3 MSUs) using 39 bit encoded string (counting 1's in a chromosome)
def indices_initial_pop(chromosome):
return {i+1 for i,c in enumerate(chromosome) if c=='1'}
#setting dynamic locations according to Chromosomes
def intial_population_bit_to_int(df_pop_initial_list):
for x in range(0, len(df_pop_initial_list), 1):
chrome = df_pop_initial_list[x]
msu_locations = indices_initial_pop(chrome)
initial_chromosomes_list.append(msu_locations)
return initial_chromosomes_list
initial_chromosomes_in_int_list = intial_population_bit_to_int(df_pop_initial_list)
print (initial_chromosomes_in_int_list)
输出:[set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]
为什么它给我一个 set()?
【问题讨论】:
-
indices_initial_pop返回集合,因为您要返回集合理解的结果(由于{})。你想要一个列表理解吗?此外,如果它们都是空的,则表示if c=='1'永远不会为真。 -
@Carcigenicate 我想要一个类似这样的列表
[{24, 17, 22}, {16, 4, 38}, {25, 13, 38}, {32, 18, 15}] -
for循环在python中是for..in循环,所以可以直接访问元素你的列表,而不是遍历指数在range中并使用那访问元素
标签: python list jupyter-notebook genetic-algorithm genetic-programming