Raphael Vitor Raphael Vitor -4 years ago 66
Python Question

Better way to print a true group in Python

I want to make a program that returns a group of True variables that i found in my program. Like this:

1 = True
2 = True
3 = False
4 = False
5 = True


What I want is to return as a print

The true numbers are: 1, 2 and 5


Edit 1:

The code is a letter counter!

eacha letter in a group has a value.

Like
a=1
b = 2
...

If a number repeats more than 4 times, that number is a true

The group would be a name. like John in an imput.

The program reads it and gives a number for each letter.

what I am using right now is this (Preatty ugly I know, but I started programing this month...), where "a" is the amount of letter a in the name, b is the amount of b in the name....

if (a + j + s) >=4:
exe1 = 1
else:
exe1 = ""

if (b + k + t) >=4:
exe2 = 2
else:
exe2 = ""

if (c + l + u) >=4:
exe3 = 3
else:
exe3 = ""

if (d + m + v) >=4:
exe4 = 4
else:
exe4 = ""

if (e + n + w) >=4:
exe5 = 5
else:
exe5 = ""

if (f + o + x) >=4:
exe6 = 6
else:
exe6 = ""

if (g + p + y) >=4:
exe7 = 7
else:
exe7 = ""

if (h + q + z) >=4:
exe8 = 8
else:
exe8 = ""

if (i + r) >=4:
exe9 = 9
else:
exe9 = ""


print("Excesses:", exe1, exe2, exe3, exe4, exe5, exe6, exe7, exe8, exe9)

Answer Source

Why don't you use a dictionary? Take a look in this code as an example.

dict1 = {1:'True', 2:'True', 3:'False', 4:'False', 5:'True'}

list = []

for k, v in dict1.items():
    if v == 'True':
        #print(k, sep=' ', end=',')
        list.append(k)


print(list)

Now you the list - although inside brackets and I don't know how to remove them since they are part of the list... Maybe iteration over it again should solve the problem, although I am not a truly expert in Python.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download