pythondataframebit-manipulationgenetic-algorithmgenetic-programming

counting 1's in a bit string to keep positions in a variable and then accessing each value


I am finding 1's in a bit string and then storing them.

Sample Data:

enter image description here

Sample code:

def indices(chromosome):
    return {i for i,c in enumerate(chromosome) if c=='1'}

for ind in df_initial_pop['initial_pop'].index:
    locations = indices(df_initial_pop['initial_pop'] [ind])
    print (locations)

Output:

{32, 29, 31}
{8, 34, 23}
{34, 35, 31}
{17, 14, 31}
{26, 19, 34}

Now, I want to access 32, 29, and 31 and store each of them in a separate variable. Is it possible?


Solution

  • Sure it is possible, but using individual variables implies that the number of elements in your set is fixed.

    s = {32, 29, 31}
    a,b,c = s
    # Now a=32, b=29, c=31