Python Double List

Asked 2 years ago, Updated 2 years ago, 93 views

It doesn't work on the sauce I made at all, and it doesn't come out even if I'm brain-rolling all day today.

import random

table1=[[0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0],
        [0,0,0,0,0,0,0,0,0,0]]

for i in range(0,10):
    print("\n")
    for j in range(0,10):
        a=random.randrange(1,100)
        table1[i][j]=a         
for i in range(0,10):
    print("\n")
    for j in range(0,10):
        a=random.randrange(1,100)
        table1[i][j]=a
        if i>=1 and j>=1:
            for e in range(i,-1,-1):
                for f in range(j,-1,-1):
                    if table1[i][j]==table1[e][f]:
                        a=random.randrange(1,100)
                        table1[i][j]=a
                if e>=1:
                    for u in range(9,-1,-1):
                        if table1[e-1][u]==table1[i][j]:
                            a=random.randrange(1,100)
                            table1[i][j]=a    


        print(table1[i][j],end=" ")

I've tried this, but the duplicate value appears on the double list. Now I have no idea what to do.

I hope you can tell me easier and simpler ways or things that need to be fixed!

python multidimensional-list

2022-09-22 20:12

3 Answers

import random

numContainer = random.sample(range(100), 20)

numContainer[:10]
Out[32]: [23, 6, 26, 55, 60, 50, 85, 84, 0, 58]

numContainer[10:]
Out[33]: [7, 77, 81, 11, 35, 98, 62, 80, 71, 15]

It means to make 20 numbers as above and cut and write 10 each.

The source of the question seems to need 100 numbers.

Then

random.sample(range(100), 100)

It means to make 100 pieces at once and cut them and use them.


2022-09-22 20:12

Make as many numbers as you need with one use instead of using random every time.

import random

random.sample(range(100), 10)

This returns 10 non-overlapping numbers underwater from 1 to 100 to the list.

You can take a number from the list and use it one by one.


2022-09-22 20:12

b=random.sample(range(100),100)
for i in range(0,10): print("\n") for j in range(0,10): a=random.randrange(1,100) table1[i][j]=a for e in range(0,99): if table1[i][j]==b[e]: b.remove(b[e]) b.insert(e,0) else: a=random.randrange(1,100) print(table1[i][j],end=" ")

Even if I squeeze it like this, there's a duplicate number.


2022-09-22 20:12

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.