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?

6
  • 2
    how you create those lists? (b_421, b_521 ...) Commented Nov 25, 2019 at 6:48
  • 1
    What list ends up containing wrong values? Add a sample Commented Nov 25, 2019 at 6:48
  • please share the whole code. Commented Nov 25, 2019 at 6:49
  • 1
    If you are declaring your list like b_421=b_521=[] then they will contain all values Commented Nov 25, 2019 at 6:52
  • First, it is very likely that you created one empty list instead of many empty lists. Second, make it a list of lists instead of 12 separate named lists. Third, float(421) is simply 421., and it is no better that 421 itself. Commented Nov 25, 2019 at 6:53

2 Answers 2

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': []}
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

I believe I that is exactly how I assigned the lists
if this answer helped you, can you mark this as answered?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.