Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

finding indexes of 1's in a bit string and storing these indexes in a list — Index should be started from 1 not 0

I am finding indexes of 1's in a bit string successfully. However, these indexes start from 0.

Sample Data:

initial_pop
000000000000000000000000000001011000000
000000001000000000000001000000000010000
000000000000000000000000000000010011000
000000000000001001000000000000010000000
000000000000000000010000001000000010000
1000000000100000000010000000000000000000
1000000010000000000001000000000000000000
1001000000000000000010000000000000000000
000000000000100000000000100000000000010
000000000110000000000000000000001000000

Now, If we look at the 6th, 7th, and 8th data rows, values are placed at index 0 by default.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

My sample code:

#Getting 1's index in a bit string
def intial_population(df_initial_pop):
    for ind in df_initial_pop['initial_pop'].index:
        msu_locations = indices(df_initial_pop['initial_pop'] [ind])
        initial_chromosomes_list.append(msu_locations)

#Calling function
intial_population(df_initial_pop)

Output: (print (initial_chromosomes_list))

[{32, 29, 31}, {8, 34, 23}, {34, 35, 31}, {17, 14, 31}, {26, 19, 34}, {0, 10, 20}, {0, 8, 21}, {0, 3, 20}, {24, 12, 37}, {32, 9, 10}]

As you can see in the above ouput, the index values are starting from 0.

Is it possible that I start my indexes from 1?

>Solution :

You are good there, just make an increment while iterating.

This should work:

return {i+1 for i,c in enumerate(chromosome) if c=='1'} 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading