0

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()?

  • 2
    `indices_initial_pop` returns sets because you're returning the result of a set comprehension (due to `{}`). Did you want a list comprehension? Also, if they're all empty, that indicates `if c=='1'` is never true. – Carcigenicate Nov 20 '22 at 14:53
  • @Carcigenicate I want a list something like this `[{24, 17, 22}, {16, 4, 38}, {25, 13, 38}, {32, 18, 15}]` –  Nov 20 '22 at 15:00
  • `for` loops in python are `for..in` loops, so they can directly access _elements_ of your list, instead of iterating over _indices_ in a `range` and using _that_ to access the elements – Pranav Hosangadi Nov 20 '22 at 15:01
  • @Carcigenicate what does that have to do with the question? OP is right about using a set comprehension in `indices_initial_pop`, since this method returns only a single entry (a set) in the final list. And that list is constructed in `intial_population_bit_to_int` – Alex P Nov 20 '22 at 15:25
  • [How to step through Python code to help debug issues?](https://stackoverflow.com/questions/4929251/how-to-step-through-python-code-to-help-debug-issues) If you are using an IDE **now** is a good time to learn its debugging features Or the built-in [Python debugger](https://docs.python.org/3/library/pdb.html). Printing *stuff* at strategic points in your program can help you trace what is or isn't happening. [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – wwii Nov 20 '22 at 16:50

1 Answers1

0

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}]

Alex P
  • 1,105
  • 6
  • 18