finding indexes of 1's in a bit string and storing these indexes in a list — Index should be started from 1 not 0
Question:
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.
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
?
Answers:
You can shift the initial data frame index by 1
df_initial_pop.index += 1
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'}
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.
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
?
You can shift the initial data frame index by 1
df_initial_pop.index += 1
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'}