1

I'm trying to form several lists, and sort values of another list into each of these based on a conditional.

I have the following:

b_421, b_521, b_621, b_721, b_821, b_921, b_1021, b_1121, b_1221, b_1321, b_1421, b_1520 = ([] for i in range(12))

for n in x_list:
    if n < float(421):
        b_421.append(n)
    elif float(421) <= n < float(521):
        b_521.append(n)
    elif float(521) <= n < float(621):
        b_621.append(n)
    elif float(621) <= n < float(721):
        b_721.append(n)
    elif float(721) <= n < float(821):
        b_821.append(n)
    elif float(821) <= n < float(921):
        b_921.append(n)
    elif float(921) <= n < float(1021):
        b_1021.append(n)
    elif float(1021) <= n < float(1121):
        b_1121.append(n)
    elif float(1121) <= n < float(1221):
        b_1221.append(n)
    elif float(1221) <= n < float(1321):
        b_1321.append(n)
    elif float(1321) <= n < float(1421):
        b_1421.append(n)
    elif float(1421) <= n < float(1520):
        b_1520.append(n)

However, my lists all contain all of the elements from the original list x_list.

What am I doing wrong?

EDITED:

Even if I define my lists in the following way:

b_421=[]
b_521=[]
b_621=[]
b_721=[]
b_821=[]
b_921=[]
b_1021=[] 
b_1121=[] 
b_1221=[] 
b_1321=[] 
b_1421=[] 
b_1520=[]

I still get b_421 to contain all the numbers within my original list. The original list was created from a pandas dataFrame column, could this be the issue?

x_list = df['col1'].tolist()

The only way I get this to give me my desired output, which is each list containing the values within the desired ranges, is if I define the lists individually as I did above and then create a separate for, if combo for each list. As so:

for n in x_list:
    if n < float(421):
        b_421.append(n)
for n in x_list:
    if float(421) <= n < float(521):
        b_521.append(n)  
for n in x_list:        
    if float(521) <= n < float(621):
        b_621.append(n)
for n in x_list:  
    if float(621) <= n < float(721):
        b_721.append(n)
for n in x_list:  
    if float(721) <= n < float(821):
        b_821.append(n)
for n in x_list:  
    if float(821) <= n < float(921):
        b_921.append(n)
for n in x_list:  
    if float(921) <= n < float(1021):
        b_1021.append(n)
for n in x_list:  
    if float(1021) <= n < float(1121):
        b_1121.append(n)
for n in x_list:  
    if float(1121) <= n < float(1221):
        b_1221.append(n)
for n in x_list:  
    if float(1221) <= n < float(1321):
        b_1321.append(n)
for n in x_list:  
    if float(1321) <= n < float(1421):
        b_1421.append(n)
for n in x_list:  
    if float(1421) <= n < float(1520):
        b_1520.append(n)

This is insane to me - maybe my environment is just broken?

EdAlex
  • 25
  • 8

2 Answers2

1

The code seems to be working fine. I guess you have defined the lists properly. Here is a better approach: You can create a dictionary to store all your lists:

steps = [421, 521, 621, 721, 821, 921, 1021, 1121, 1221, 1321, 1421, 1520]
output = { 'b_' + str(value): [] for value in steps }
steps.insert(0, 0)
ranges = [[steps[i], steps[i + 1]] for i in range(len(steps) - 1)]
for n in x_list:
    for range in ranges:
        if float(range[0]) <= n < float(range[1]):
             output['b_' + str(range[1])].append(n)

This is not the most efficient solution since it runs the loop even if it is added to the list. Here is a sample input and output:

Input

x_list = [1, 123, 345, 567, 678, 1000]

Output

output = {'b_421': [1, 123, 345], 'b_521': [], 'b_621': [567], 'b_721': [678], 'b_821': [], 'b_921': [], 'b_1021': [1000], 'b_1121': [], 'b_1221': [], 'b_1321': [], 'b_1421': [], 'b_1520': []}
Yedhu Krishnan
  • 1,225
  • 15
  • 31
0

I'm assuming that you created all the lists in this way:

b_421 = b_521 = []

Assigning one variable to another makes them point to the same area in the memory, then any change that done to one variable also reflect the other.

This topic explained very well here.

FYI - how to approach a correct assignment to multiple lists.

Emanuel
  • 640
  • 1
  • 7
  • 25