1

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.

asked Feb 7, 2016 at 23:23
2
  • Dictionaries, e.g. {'name': '#Home', 'users': 50, ...}? Or collections.namedtuple. Commented Feb 7, 2016 at 23:27
  • Your data structure is rather unclear. Can dicts help you here? Possibly a dict that has a list or tuple as its values. Otherwise, you may have to go a step further and see if something like Numpys structured arrays or Pandas DataFrame can suit your needs. Commented Feb 7, 2016 at 23:27

3 Answers 3

2

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
answered Feb 7, 2016 at 23:28
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, that seems to do what I want! Easy and simple answer.
0

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')
answered Feb 7, 2016 at 23:31

Comments

0

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']
answered Feb 7, 2016 at 23:34

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.