Jump to content

So I've got a basic question. Basically lost to be honest. 

 

How would I make a list with columns using a "for" loop without and library functions. I need these numbers 1 3 4 5 8 9 1 1

Output:

 

1    3

4    5

8    9

1    1

 

and then 0 3 9 1 2 1

 

Output:

 

0   3   9  

1   2   1

Spoiler

 

LTT's Fastest single core CineBench 11.5/15 score on air with i7-4790K on air

Main Rig

CPU: i7-4770K @ 4.3GHz 1.18v, Cooler: Noctua NH-U14S, Motherboard: Asus Sabertooth Mark 2, RAM: 16 GB G.Skill Sniper Series @ 1866MHz, GPU: EVGA 980Ti Classified @ 1507/1977MHz , Storage: 500GB 850 EVO, WD Cavier Black/Blue 1TB+1TB,  Power Supply: Corsair HX 750W, Case: Fractal Design r4 Black Pearl w/ Window, OS: Windows 10 Home 64bit

 

Plex Server WIP

CPU: i5-3570K, Cooler: Stock, Motherboard: ASrock, Ram: 16GB, GPU: Intel igpu, Storage: 120GB Kingston SSD, 6TB WD Red, Powersupply: Corsair TX 750W, Case: Corsair Carbide Spec-01 OS: Windows 10

 

Lenovo Legion Laptop

CPU: i7-7700HQ, RAM: 8GB, GPU: 1050Ti 4GB, Storage: 500GB Crucial MX500, OS: Windows 10

 

 

Link to comment
https://linustechtips.com/topic/1040534-python-number-list-with-columns/
Share on other sites

Link to post
Share on other sites

Just print them one by one and every time a counter reaches 2 or 3 or however many columns you want print a '\n' to go to a new line.

NEW PC build: Blank Heaven   minimalist white and black PC     Old S340 build log "White Heaven"        The "LIGHTCANON" flashlight build log        Project AntiRoll (prototype)        Custom speaker project

Spoiler

Ryzen 3950X | AMD Vega Frontier Edition | ASUS X570 Pro WS | Corsair Vengeance LPX 64GB | NZXT H500 | Seasonic Prime Fanless TX-700 | Custom loop | Coolermaster SK630 White | Logitech MX Master 2S | Samsung 980 Pro 1TB + 970 Pro 512GB | Samsung 58" 4k TV | Scarlett 2i4 | 2x AT2020

 

Link to post
Share on other sites

3 minutes ago, Br3tt96 said:

So I've got a basic question. Basically lost to be honest. 

 

How would I make a list with columns using a "for" loop without and library functions. I need these numbers 1 3 4 5 8 9 1 1

Output:

 

1    3

4    5

8    9

1    1

 

and then 0 3 9 1 2 1

 

Output:

 

0   3   9  

1   2   1

umm, without doing any math, I'm lazy at the moment, i would drop those puppies into a 1d array, then iterate though it with the for loop, use a temp var as an index counter. Edit: dose python support arrays?

Link to post
Share on other sites

5 minutes ago, Enderman said:

Just print them one by one and every time a counter reaches 2 or 3 or however many columns you want print a '\n' to go to a new line.

 

4 minutes ago, slashqpbyte said:

umm, without doing any math, I'm lazy at the moment, i would drop those puppies into a 1d array, then iterate though it with the for loop, use a temp var as an index counter.

So something like this

 

for i in mat_a:
    for j in mat_a[a]:
        print(j, end= " ")
    a = a + 1 
print("\n")

 

 

still prints basically  it like 1 3 4 5 8 9 1 1

Spoiler

 

LTT's Fastest single core CineBench 11.5/15 score on air with i7-4790K on air

Main Rig

CPU: i7-4770K @ 4.3GHz 1.18v, Cooler: Noctua NH-U14S, Motherboard: Asus Sabertooth Mark 2, RAM: 16 GB G.Skill Sniper Series @ 1866MHz, GPU: EVGA 980Ti Classified @ 1507/1977MHz , Storage: 500GB 850 EVO, WD Cavier Black/Blue 1TB+1TB,  Power Supply: Corsair HX 750W, Case: Fractal Design r4 Black Pearl w/ Window, OS: Windows 10 Home 64bit

 

Plex Server WIP

CPU: i5-3570K, Cooler: Stock, Motherboard: ASrock, Ram: 16GB, GPU: Intel igpu, Storage: 120GB Kingston SSD, 6TB WD Red, Powersupply: Corsair TX 750W, Case: Corsair Carbide Spec-01 OS: Windows 10

 

Lenovo Legion Laptop

CPU: i7-7700HQ, RAM: 8GB, GPU: 1050Ti 4GB, Storage: 500GB Crucial MX500, OS: Windows 10

 

 

Link to post
Share on other sites

3 minutes ago, Br3tt96 said:

 

So something like this

 

for i in mat_a:
    for j in mat_a[a]:
        print(j, end= " ")
    a = a + 1 
print("\n")

 

 

still prints basically  it like 1 3 4 5 8 9 1 1

add an if statement, every time its an even number, print a new line

Link to post
Share on other sites

27 minutes ago, slashqpbyte said:

add an if statement, every time its an even number, print a new line

#include <iostream>

using namespace std;

int main()
{
    //cout<<"Goodbye World";
    const int sizeOne = 8;
    const int sizeTwo = 6;

    int setOne[sizeOne] = {1,3,4,5,8,9,1,1};
    int setTwo[sizeTwo] = {0,3,9,1,2,1};
    
    //----- set one -----
    for(int i = 0; i < sizeOne; i++)
    {
        cout << setOne[i] << " ";
        if((i%2) == 1)
        {
            cout << endl; //newline
        }
    }

    //----- set two -----
    for(int i = 0; i < sizeTwo; i++)
    {
        cout << setTwo[i] << " ";
        if(i == 2)
        {
            cout << endl; //newline
        }
    }    
    return 0;
}

I don't know python, but hopefully this helps.

Link to post
Share on other sites

15 minutes ago, slashqpbyte said:

#include <iostream>

using namespace std;

int main()
{
    //cout<<"Goodbye World";
    const int sizeOne = 8;
    const int sizeTwo = 6;

    int setOne[sizeOne] = {1,3,4,5,8,9,1,1};
    int setTwo[sizeTwo] = {0,3,9,1,2,1};
    
    //----- set one -----
    for(int i = 0; i < sizeOne; i++)
    {
        cout << setOne[i] << " ";
        if((i%2) == 1)
        {
            cout << endl; //newline
        }
    }

    //----- set two -----
    for(int i = 0; i < sizeTwo; i++)
    {
        cout << setTwo[i] << " ";
        if(i == 2)
        {
            cout << endl; //newline
        }
    }    
    return 0;
}

I don't know python, but hopefully this helps.

Helpful very much so, you introduced the concept of modulo without tell op how to complete his homework.

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to post
Share on other sites

29 minutes ago, slashqpbyte said:

#include <iostream>

using namespace std;

int main()
{
    //cout<<"Goodbye World";
    const int sizeOne = 8;
    const int sizeTwo = 6;

    int setOne[sizeOne] = {1,3,4,5,8,9,1,1};
    int setTwo[sizeTwo] = {0,3,9,1,2,1};
    
    //----- set one -----
    for(int i = 0; i < sizeOne; i++)
    {
        cout << setOne[i] << " ";
        if((i%2) == 1)
        {
            cout << endl; //newline
        }
    }

    //----- set two -----
    for(int i = 0; i < sizeTwo; i++)
    {
        cout << setTwo[i] << " ";
        if(i == 2)
        {
            cout << endl; //newline
        }
    }    
    return 0;
}

I don't know python, but hopefully this helps.

 

13 minutes ago, vorticalbox said:

Helpful very much so, you introduced the concept of modulo without tell op how to complete his homework.

I mean it's closer than what I had. Probably something in my loops

 

image.png.202f350071fd71611f734c2db6d27374.png

Spoiler

 

LTT's Fastest single core CineBench 11.5/15 score on air with i7-4790K on air

Main Rig

CPU: i7-4770K @ 4.3GHz 1.18v, Cooler: Noctua NH-U14S, Motherboard: Asus Sabertooth Mark 2, RAM: 16 GB G.Skill Sniper Series @ 1866MHz, GPU: EVGA 980Ti Classified @ 1507/1977MHz , Storage: 500GB 850 EVO, WD Cavier Black/Blue 1TB+1TB,  Power Supply: Corsair HX 750W, Case: Fractal Design r4 Black Pearl w/ Window, OS: Windows 10 Home 64bit

 

Plex Server WIP

CPU: i5-3570K, Cooler: Stock, Motherboard: ASrock, Ram: 16GB, GPU: Intel igpu, Storage: 120GB Kingston SSD, 6TB WD Red, Powersupply: Corsair TX 750W, Case: Corsair Carbide Spec-01 OS: Windows 10

 

Lenovo Legion Laptop

CPU: i7-7700HQ, RAM: 8GB, GPU: 1050Ti 4GB, Storage: 500GB Crucial MX500, OS: Windows 10

 

 

Link to post
Share on other sites

Hi @Br3tt96 , the following Python code should achieve your desired result:

if __name__ == "__main__":
    L1 = [1, 3, 4, 5, 8, 9, 1, 1]
    L2 = [0, 3, 9, 1, 2, 1]

    def print_output(list, multiple):
        """
        Function to print a list of numbers and return a new line every X amount of printed values.

        Parameter 1 (list): Desired list to enumerate over and process.
        Parameter 2 (multiple): After printing X values where X=multiple, a newline will be printed.
        """
        # Enumerates over out list, while keeping track of an index, which starts at value 1, not 0.
        for index, item in enumerate(list, start=1):
            # Print the current value with a space. end="" ensures no newline will be printed.
            print(str(item) + " ", end="")
            # We want to return every X amount of lines, where X=multiple.
            # The modulus result will only be equal to 0 when our index % multiple has no remainder.
            if index % multiple == 0:
                # Here we just print a newline
                print()

    # Lets try out our function with the input lists you provided.
    # I pass the appropriate multiple in based on your desired output.
    print("First list:")
    print_output(L1, 2)

    print("Second list:")
    print_output(L2, 3)

As opposed to example C++ posts above, instead of writing the desired code multiple times in slightly different formatted loops, we create a function to reuse code. The function accepts 2 parameters, the first being the list you want to process, and the second being a multiple. The multiple is an integer value which determines how often the printing function will return a new line. So if you want to produce a new line every 2 lines, pass in a multiple of 2.

 

Here is the result of running the above Python code.

First list:
1 3 
4 5 
8 9 
1 1 
Second list:
0 3 9 
1 2 1 

 

The reason your code isn't producing expected output in one of your follow up posts above: It's most likely an off by 1 error. For example, in my code if you change "start=1" to "start=0" in the for loop, you'll produce a similar incorrect output. I would check your code for off by 1 errors by debugging and stepping through your code.

 

Hopefully this post helped.

Current PC build: [CPU: Intel i7 8700k] [GPU: GTX 1070 Asus ROG Strix] [Ram: Corsair LPX 32GB 3000MHz] [Mobo: Asus Prime Z370-A] [SSD: Samsung 970 EVO 500GB primary + Samsung 860 Evo 1TB secondary] [PSU: EVGA SuperNova G2 750w 80plus] [Monitors: Dual Dell Ultrasharp U2718Qs, 4k IPS] [Case: Fractal Design R5]

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×