I am pretty new to Python, and so far I haven't stumbled upon any problems, until now. I need a data structure that allows me to store multi dimensional data. For example, I need my IRC bot to store all users and modes on a channel. So it would look something like this:
[Name][Users][Nicklist][Modes][Topic][etc]
[#Home][50]['Nick1', 'Nick2', 'Nick3', 'etc'][+nt][topic here][more info]
[#Otherchan][10]['Nick1', 'Nick2', 'Nick3', 'etc'][+][topic][more info]
and I need to recall and edit the information via the channel name. I been looking into lists and arrays but I have not found a convenient way to accomplish this.
If there is an even better way for this, that'd be great too. I hope I'm explaining myself well.
3 Answers 3
It sounds like you want to use the dict object.
d = {}
d['#Home'] = {}
d['#Home']['Users'] = 50
d['#Home']['Nicklist'] = ['Nick 1', 'Nick 2']
and so on
You can retrieve the values just like an array
print d['#Home']['Users']
50
1 Comment
dictionaries (mutable):
test = {'data_name': data_value, 'data_name_2', data_value_2}
Lists (mutable):
test = ['a', 1, 2, [1, 2, 3], (1, 2)]
Tuples (immutable - cannot be modified or appended to afterwards):
test = (1, 2, 3, [5, 6, 7], 'a', 'b')
Comments
You should look into using a dictionary, which maps keys and values to each other. The syntax would be something like this:
d = {}
d['#Home'] = 50
d['Nicklist'] = ['Nick1', 'Nick2','Nick3','etc']
Also note for dicts, the keys and values can essentially be any object (as long as the keys are unique), and dicts can be embedded within dicts, for example:
d= {}
d['#Home'] = {} #made a dict for the '#Home' key
d['#Home']['Nicklist'] = ['Nick1', 'Nick2','Nick3','etc']
{'name': '#Home', 'users': 50, ...}? Orcollections.namedtuple.