简体   繁体   中英

How do you make a set of variables all equal the same number in python?

I am trying to print a board like this

   board = [
   [a1, a2, a3, a4, a5, a6, a7, a8, a9],
   [b1, b2, b3, b4, b5, b6, b7, b8, b9],
   [c1, c2, c3, c4, c5, c6, c7, c8, c9],
   [d1, d2, d3, d4, d5, d6, d7, d8, d9],
   [e1, e2, e3, e4, e5, e6, e7, e8, e9],
   [f1, f2, f3, f4, f5, f6, f7, f8, f9],
   [g1, g2, g3, g4, g5, g6, g7, g8, g9],
   [h1, h2, h3, h4, h5, h6, h7, h8, h9],
   [i1, i2, i3, i4, i5, i6, i7, i8, i9]
   ]

But I have to name every single variable and equal it to zero.

   a1 = 0 a2 = 0 a3 = 0 a4 = 0 a5 = 0 a6 = 0 a7 = 0 a8 = 0 a9 = 0 b1 = 0

etc. all the way up to i9 This is obviously obnoxious, how can I make this smaller without changing the board. I do not want to print a new board, but just simply make the variables a1-9 to i1-9 more compact

do you think this could work?

 myArray = a1=a2=a3=a4=a5=a6=a7=a8=a9=b1=b2=b3=[0]

You should use lists for that and loops.

a=[0]*9
b=[0]*9
.
.
.

Actually you could make a list of list and you'll have it all in one variable easily accesible.

myList = [ [0] * 9 for _ in range(9)]

You can make a 2-D list where a row represents characters ( 'a'-'i' ) and column to represent numbers ( 1-9 )

matrix=[[0 for _ in range(9)] for _ in range(9)]

  1 ,2 ,3 ,4 ,5 ,6 ,7, 8 ,9
a[[0, 0, 0, 0, 0, 0, 0, 0, 0],
b [0, 0, 0, 0, 0, 0, 0, 0, 0],
c [0, 0, 0, 0, 0, 0, 0, 0, 0],
d [0, 0, 0, 0, 0, 0, 0, 0, 0],
e [0, 0, 0, 0, 0, 0, 0, 0, 0],
f [0, 0, 0, 0, 0, 0, 0, 0, 0],
g [0, 0, 0, 0, 0, 0, 0, 0, 0],
h [0, 0, 0, 0, 0, 0, 0, 0, 0],
i [0, 0, 0, 0, 0, 0, 0, 0, 0]]

And you can index them using matrix[i][j] .

you can use dictionary:

data = {}
for i in range(10):
    for letter in list('abcdefghi'):
        data.update({letter+str(i):0})

{'a0': 0, 'b0': 0, 'c0': 0, 'd0': 0, 'e0': 0, 'f0': 0, 'g0': 0, 'h0': 0, 'i0': 0, 'a1': 0, 'b1': 0, 'c1': 0, 'd1': 0, 'e1': 0, 'f1': 0, 'g1': 0, 'h1': 0, 'i1': 0, 'a2': 0, 'b2': 0, 'c2': 0, 'd2': 0, 'e2': 0, 'f2': 0, 'g2': 0, 'h2': 0, 'i2': 0, 'a3': 0, 'b3': 0, 'c3': 0, 'd3': 0, 'e3': 0, 'f3': 0, 'g3': 0, 'h3': 0, 'i3': 0, 'a4': 0, 'b4': 0, 'c4': 0, 'd4': 0, 'e4': 0, 'f4': 0, 'g4': 0, 'h4': 0, 'i4': 0, 'a5': 0, 'b5': 0, 'c5': 0, 'd5': 0, 'e5': 0, 'f5': 0, 'g5': 0, 'h5': 0, 'i5': 0, 'a6': 0, 'b6': 0, 'c6': 0, 'd6': 0, 'e6': 0, 'f6': 0, 'g6': 0, 'h6': 0, 'i6': 0, 'a7': 0, 'b7': 0, 'c7': 0, 'd7': 0, 'e7': 0, 'f7': 0, 'g7': 0, 'h7': 0, 'i7': 0, 'a8': 0, 'b8': 0, 'c8': 0, 'd8': 0, 'e8': 0, 'f8': 0, 'g8': 0, 'h8': 0, 'i8': 0, 'a9': 0, 'b9': 0, 'c9': 0, 'd9': 0, 'e9': 0, 'f9': 0, 'g9': 0, 'h9': 0, 'i9': 0}

I really discourage to use anything like this even though it seems to work in Python 3.

for c in 'abcdefghi':
    for i in range(1,10):
        locals()[c+str(i)] = 0

print(a1,a3,i9)

Prints: 0 0 0

You can use string.ascii_lowercase to generate a sequence from a to i

from collections import defaultdict
import string

variables = defaultdict(int)
for value in string.ascii_lowercase[0:9]:
    for i in range(10):
        variables[value + str(i)]

print(variables)

OR

 import string
    variables={}
    for value in string.ascii_lowercase[0:9]:
        for i in range(10):
            variables.update({value + str(i): 0})

    print(variables)

For such a number of variables, you might have wanted to use an array or a list. However, in the case that the variables are already there for whatever reason, you still might want to add them to an array.

myArray = [a1, a2, a3, a4,..., i9] #There is no shortcut. You have to hardcode them.
{element = 0 for element in myArray}
#Taking advantage of list comprehensions to do the work.

The second line is not the same with this:

myArray = [0 for element in myArray]

The first one changes the variables you put in your array (think pass-by-reference), while the second one creates a new array full of zeroes with the same number of elements. This change would make sense if you used objects (classes) instead of primitive data types. In your example, those are integers so this does not apply. (Which means that you can do both approaches)

Adding your variables to the array would also allow you to do similar work in the future.

I'm pretty sure everyone here would recommend using an array in the first place and appending values to it, possibly with a loop, instead of declaring variables for all those.

Although Ch3ster's answer makes more sense, if you really need variables like that you can use python's globals() function. This function returns a dictionary of the name and value of every variable in the scope, and you can modify it like this:

globals()[<the_name_of_the_new_variable]=<the_new_value>

(replace with the variable name, and with the value of the variable) This creates a new variable, with the name and value entered, and you can use it for your purposes like this:

letters="abcdefghi"
numbers="123456789"
for l in letters:
    for n in numbers:
        globals()[l+n]=0

This is the dirty way to do it, but has the equivalent of creating a new variable for every combination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM