Home > Enterprise >  I have a defined a variable inside a for loop, but when printing it outside of the loop it doesn
I have a defined a variable inside a for loop, but when printing it outside of the loop it doesn

Time:01-01

Here is the code:

count_1 = 0 
count_0 = 0 
list = ('001111011011','000110001010','011010111111')`
for i in list:
index = 0 
y = i[index]

if y == "1":
    count_1 = count_1   1
if y == "0":
    count_0 = count_0   1

if count_1 > count_0:
    for i in list:
        final_after_1 = []
        if i[0] == "1":
            final_after_1.append(i)
            formatted = (','.join(final_after_1))
if count_0 > count_1:
    for i in list:
        final_after_1 = []
        if i[0] == "0":
            final_after_1.append(i)
            formatted = (','.join(final_after_1))
if count_0 == count_1:
    for i in list:
        final_after_1 = []
        if i[0] == "1":
            final_after_1.append(i)
            print(final_after_1)
            formatted = (','.join(final_after_1))
    

print(formatted)

(Apologies in advance if this question is worded badly, this is my first time asking a question).

This piece of code is working fine except for this one issue. It is designed to identify the first index of each 12-digit number in the list, and then work out if a 1 or 0 is more common in this position. It then selects all the numbers with the more common number in the first position and adds them to a list. I want to print this list at the end of the program.

I have defined a variable (called formatted) to be equal to a list of various numbers. When I print it out within the loop I have defined it in, it prints all the numbers that should be in the list, like this:

When I print it outside of the loop as in the code above it returns only the final number:

011010111111

Whereas printing it inside the loop like this:

if count_0 > count_1:
        for i in list:
            final_after_1 = []
            if i[0] == "0":
                final_after_1.append(i)
                formatted = (','.join(final_after_1))
                print(formatted)

does return this full desired list:

001111011011

000110001010

011010111111

Any ideas why this is happening?

CodePudding user response:

Within you loop(s) the value of formatted is updated for each iteration. After the last iteration it is no longer updated and that last value is the output of the last print statement.

A simpler example:

for x in range(100):
    pass//looping over, x is 0..99

print(x)

This will print out 99, the last value held by the variable "x".

CodePudding user response:

Problably your code is updateing the variable for each iteration so in a for loop you need to append the values and not overwrite them, for example:

a = 0
b = 0

for i in 10:
    a = 1
    b = b   1 # using the last value

print(a) # 1
print(b) # 9

First of all you shouldn't use "list" as a variable name because is a built-in name to instanciate lists or arrays. In second your code is repeated 3 times just for count a bit, let me show a better way with list compreenssions:

l = ('001111011011','000110001010','011010111111')

first_elements = list()
for x in l:
    v = x[0] # first element
    first_elements.append(int(v))
# [0,0,0]

count_0 = first_elements.count(0) 
# count_0 = 3

count_1 = first_elements.count(1) 
# count_1 = 0

Using list compreenssion

first_elements = [int(x[0]) for x in l]
# [0,0,0]

References: list compreenssions, list, list.count

  • Related