Iterating a list: getting values as[set(), set(), set(), set(), set()]

Question:

I have a list (df_pop_initial_list), and it looks like this:

[['000000000000000000000000000001011000000'],
 ['000000001000000000000001000000000010000'],
 ['000000000000000000000000000000010011000'],
 ['000000000000001001000000000000010000000'],
 ['000000000000000000010000001000000010000'],
 ['1000000000100000000010000000000000000000'],
 ['1000000010000000000001000000000000000000'],
 ['1001000000000000000010000000000000000000'],
 ['000000000000100000000000100000000000010'],
 ['000000000110000000000000000000001000000'],
 ['000000101000000010000000000000000000000'],
 ['000000000000001000000010000100000000000'],
 ['000000000000000010000101000000000000000'],
 ['000000001000100000000000000000000100000'],
 ['000000100000000000000000010000001000000'],
 ['000000000000001100000000000010000000000'],
 ['010000000000000000000000000001001000000'],
 ['000000010100000001000000000000000000000'],
 ['000000000000000000001000000001100000000'],
 ['000100000000000100000000000000000000010']]

I am trying to count 1’s in this 39 bits string list and converting each string value into 3 integer numbers where bits are on (mean finding 1's).

My code looks like this:

#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)

Output:
[set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set(), set()]

Why it is giving me a set()?

Asked By: LearningLogic

||

Answers:

intial_population_bit_to_int is giving a list of sets because indices_initial_pop always (with the data you use) returns an empty set. Your actual question is why indices_initial_pop returns an empty set. And the answer is because the value you pass as argument in your call, i.e. chrome, is not a string, but a list containing a single string. You can fix this by using

chrome = df_pop_initial_list[x][0]

instead of

chrome = df_pop_initial_list[x]


#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):
    initial_chromosomes_list = []
    for x in range(0, len(df_pop_initial_list), 1):
        chrome = df_pop_initial_list[x][0]
        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)

Output: [{32, 33, 30}, {24, 9, 35}, {32, 35, 36}, {32, 18, 15}, {35, 27, 20}, {1, 11, 21}, {1, 9, 22}, {1, 4, 21}, {25, 13, 38}, {33, 10, 11}, {9, 17, 7}, {23, 28, 15}, {24, 17, 22}, {9, 34, 13}, {33, 26, 7}, {16, 29, 15}, {33, 2, 30}, {8, 10, 18}, {21, 30, 31}, {16, 4, 38}]

Answered By: Alex P