Python 3.x - Creating Vertical Lists with symbols representing integers

python list
how to get string input in python 3
python print format
python dictionary
python array
how to print a list python
python format
python symbol meaning

New to python and wondered if someone could help me solve this problem I am facing. I am able to print a list that displays the information horizontally, however when I try to create a vertical list I cannot seem to implement it or find any other help that can guide me through a similar scenario.

    cat1 = 4
    cat2 = 3
    cat3 = 5
    cat4 = 3
    print(" 0 to 29:   ", end=" ")
    for i in range(cat1):
        print("*", end=" ")
    else:
        print("\n 30 to 39:  ", end=" ")
        for i in range(cat2):
            print("*", end=" ")
        else:
            print("\n 40 to 69:  ", end=" ")
            for i in range(cat3):
                print("*", end=" ")
            else:
                print("\n 70 to 100: ", end=" ")
                for i in range(cat4):
                    print("*", end=" ")
     break

This outputs: 
0 to 29:   * * * *
30 to 39:  * * *
40 to 69:  * * * * *
70 to 100: * * *

What I am trying to do is:
| 0 to 29 | 30 to 39 | 40 to 69 | 70 to 100 |
     *          *          *         *
     *          *          *         *
     *          *          *         *
     *                     *         
                           *         

Any help would be greatly appreciated! Thanks!

This code snippet will produce your desired output:

cats = [cat1, cat2, cat3, cat4]
print('| 0 to 29 | 30 to 39 | 40 to 69 | 70 to 100 |')

for i in range(max(cats)):
    c1, c2, c3, c4 = ['*' if i < cat else ' ' for cat in cats]
    print('    {}          {}          {}          {}'.format(c1, c2, c3, c4))

You can adapt the amount of spaces between the {} indent the asterisks in a different way if you want to

EDIT: As @Bram Vanroy pointed out, my for loop can be improved to use print('' + ''.join(['*' if i < cat else ' ' for cat in cats])) instead of my two lines, reducing the code to

cats = [cat1, cat2, cat3, cat4]
print('| 0 to 29 | 30 to 39 | 40 to 69 | 70 to 100 |')

for i in range(max(cats)):
    print('    ' + '          '.join(['*' if i < cat else ' ' for cat in cats]))

This tutorial will introduce you to Python input and output. learn the basics of Python 3, like working with data types, dictionaries, lists, and Python n + 100 on line 3 is invalid because n is a string and 100 is an integer. They share the same name because they are represented by the same symbol: % . 3 Python 3.x - Creating Vertical Lists with symbols representing integers Oct 25 '18 3 Decision Trees - Taking a long time to process String values but working fine for float values. How to understand?

Here you go, it will work for python 3.6 and higher

cat_dict = {'0 to 29': 4, '30 to 39': 3, '40 to 69': 5, '70 to 100': 3}

print(' | '.join(cat_dict.keys()))
for i in range(max(cat_dict.values()) + 1):
    for v in cat_dict.values():
        print(f'   {"*" if i < v else " "}       ', end = '')
    print('\n')

for python 2.7 you can use the below

cat_dict = {'0 to 29': 4, '30 to 39': 3, '40 to 69': 5, '70 to 100': 3}

print(' | '.join(cat_dict.keys()))
for i in range(max(cat_dict.values()) + 1):
    for v in cat_dict.values():
        print('   {0}       '.format("*" if i < v else " "), end = '')
    print('\n')

To get started, let's create a list that contains items of the string data type: by creating a range of index numbers separated by a colon [x:y] :. 125 390 When converting floats to integers with the int() function, Python cuts off the decimal and remaining numbers of a float to create an integer. Even though we may want to round 390.8 up to 391, Python will not do this through the int() function.

This is how I did it with formatting

cats_stats = {
    "cat1": 4,
    "cat2": 3,
    "cat3": 5,
    "cat4": 3
}


print("|   0 to 29   |   30 to 39   |   40 to 69   |   70 to 100   |")

max_cats = max(cats_stats.values())

for row in range(max_cats):
    line = ""
    for cats in cats_stats.values():
        if cats <= row:
            line += "               "
        else:
            line += "       *       "
    print(line)

Output:

|   0 to 29   |   30 to 39   |   40 to 69   |   70 to 100   |
       *              *              *              *       
       *              *              *              *       
       *              *              *              *       
       *                             *                      
                                     *                      

Index – Symbols --list <zipfile>. zipfile command line option. --listfuncs. trace command line option. --locals. unittest command line option. --main=<mainfn>. The initial expression in a list comprehension can be any arbitrary expression, including another list comprehension. Consider the following example of a 3x4 matrix implemented as a list of 3 lists of length 4: >>>. >>> matrix = [ [1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12],

\newline Ignored (escape newline) \\ Backslash (\) \e Escape (ESC) \v Vertical Tab (VT) abs(x) absolute value of x int(x) x converted to integer long(x) x to "a​", creates "a" local to fcn But note "a[0]=3" starts with search for "a", dir([object]) If no args, return the list of names in current local symbol table. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more python : Split string separated by a pipe symbol “|”

Numbers are created by numeric literals or as the result of built-in functions and operators. if concatenating str objects, you can build a list and use str.join() at the end or the sequence b' \t\n\r\x0b\f' (space, tab, newline, carriage return, vertical tab, form feed). This is the dictionary containing the module's symbol table. A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

Using for loop : Traverse from 0 to len(list) and print all elements of the list one by one 1 2 3 4 5; Without using loops: * symbol is use to print the list elements in a but if the list contains integers then convert it into string and then use join() function Please use ide.geeksforgeeks.org, generate link and share the link here. 929.24 With integers and floating-point numbers, it is important to keep in mind that 3 ≠ 3.0, as 3 refers to an integer while 3.0 refers to a float.. Booleans. The Boolean data type can be one of two values, either True or False.

Comments
  • Probably needs to be if i < cat?
  • You can optimise by using print(' ' + ' '.join(['*' if i < cat else ' ' for cat in cats])) instead of the variable unpacking and the string formatting. Note: Stack Overflow seems to trim the number of spaces that I write as a comment. First four spaces, and ten spaces in the join.