I've been playing with Python and I have this list that I need worked out. Basically I type a list of games into the multidimensional array and then for each one, it will make 3 variables based on that first entry.
Array that is made:
Applist = [
['Apple', 'red', 'circle'],
['Banana', 'yellow', 'abnormal'],
['Pear', 'green', 'abnormal']
]
For loop to assign each fruit a name, colour and shape.
for i in Applist:
i[0] + "_n" = i[0]
i[0] + "_c" = i[1]
i[0] + "_s" = i[2]
When doing this though, I get a cannot assign to operator message. How do I combat this?
The expected result would be:
Apple_n == "Apple"
Apple_c == "red"
Apple_s == "circle"
Etc for each fruit.
Python allows you to name variables to your liking, as long as the names follow these rules: Variable names may contain letters, digits (0-9) or the underscore character _ . Variable names must begin with a letter from A-Z or the underscore _ character. Either lowercase or uppercase letters are acceptable.
Create Python Lists In Python, a list is created by placing elements inside square brackets [] , separated by commas. A list can have any number of items and they may be of different types (integer, float, string, etc.). A list can also have another list as an item. This is called a nested list.
Since lists can contain any Python variable, it can even contain other lists.
This is a bad idea. You should not dynamically create variable names, use a dictionary instead:
variables = {}
for name, colour, shape in Applist:
variables[name + "_n"] = name
variables[name + "_c"] = colour
variables[name + "_s"] = shape
Now access them as variables["Apple_n"]
, etc.
What you really want though, is perhaps a dict of dicts:
variables = {}
for name, colour, shape in Applist:
variables[name] = {"name": name, "colour": colour, "shape": shape}
print "Apple shape: " + variables["Apple"]["shape"]
Or, perhaps even better, a namedtuple
:
from collections import namedtuple
variables = {}
Fruit = namedtuple("Fruit", ["name", "colour", "shape"])
for args in Applist:
fruit = Fruit(*args)
variables[fruit.name] = fruit
print "Apple shape: " + variables["Apple"].shape
You can't change the variables of each Fruit
if you use a namedtuple
though (i.e. no setting variables["Apple"].colour
to "green"
), so it is perhaps not a good solution, depending on the intended usage. If you like the namedtuple
solution but want to change the variables, you can make it a full-blown Fruit
class instead, which can be used as a drop-in replacement for the namedtuple
Fruit
in the above code.
class Fruit(object):
def __init__(self, name, colour, shape):
self.name = name
self.colour = colour
self.shape = shape
It would be easiest to do this with a dictionary:
app_list = [
['Apple', 'red', 'circle'],
['Banana', 'yellow', 'abnormal'],
['Pear', 'green', 'abnormal']
]
app_keys = {}
for sub_list in app_list:
app_keys["%s_n" % sub_list[0]] = sub_list[0]
app_keys["%s_c" % sub_list[0]] = sub_list[1]
app_keys["%s_s" % sub_list[0]] = sub_list[2]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With