Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #64067 > unrolled thread
| Started by | Sam <lightaiyee@gmail.com> |
|---|---|
| First post | 2014-01-16 01:41 -0800 |
| Last post | 2014-01-17 20:53 +0000 |
| Articles | 5 — 5 participants |
Back to article view | Back to comp.lang.python
Building and accessing an array of dictionaries Sam <lightaiyee@gmail.com> - 2014-01-16 01:41 -0800
Re: Building and accessing an array of dictionaries Chris Angelico <rosuav@gmail.com> - 2014-01-16 20:48 +1100
Re: Building and accessing an array of dictionaries Jussi Piitulainen <jpiitula@ling.helsinki.fi> - 2014-01-16 11:52 +0200
Re: Building and accessing an array of dictionaries Mark Lawrence <breamoreboy@yahoo.co.uk> - 2014-01-16 17:01 +0000
Re: Building and accessing an array of dictionaries Grant Edwards <invalid@invalid.invalid> - 2014-01-17 20:53 +0000
| From | Sam <lightaiyee@gmail.com> |
|---|---|
| Date | 2014-01-16 01:41 -0800 |
| Subject | Building and accessing an array of dictionaries |
| Message-ID | <ad743135-a552-4cd6-ba1f-c492bf8bbb37@googlegroups.com> |
I would like to build an array of dictionaries. Most of the dictionary example on the net are for single dictionary.
dict = {'a':'a','b':'b','c':'c'}
dict2 = {'a':'a','b':'b','c':'c'}
dict3 = {'a':'a','b':'b','c':'c'}
arr = (dict,dict2,dict3)
What is the syntax to access the value of dict3->'a'?
Thank you.
[toc] | [next] | [standalone]
| From | Chris Angelico <rosuav@gmail.com> |
|---|---|
| Date | 2014-01-16 20:48 +1100 |
| Message-ID | <mailman.5578.1389865714.18130.python-list@python.org> |
| In reply to | #64067 |
On Thu, Jan 16, 2014 at 8:41 PM, Sam <lightaiyee@gmail.com> wrote:
> I would like to build an array of dictionaries. Most of the dictionary example on the net are for single dictionary.
>
> dict = {'a':'a','b':'b','c':'c'}
> dict2 = {'a':'a','b':'b','c':'c'}
> dict3 = {'a':'a','b':'b','c':'c'}
>
> arr = (dict,dict2,dict3)
>
> What is the syntax to access the value of dict3->'a'?
Technically, that's a tuple of dictionaries, and you may want to use a
list instead:
lst = [dict, dict2, dict3]
Like any other list or tuple, you can reference them by their indices:
lst[2] is dict3
lst[2]['a'] is dict3['a']
Hope that helps!
ChrisA
[toc] | [prev] | [next] | [standalone]
| From | Jussi Piitulainen <jpiitula@ling.helsinki.fi> |
|---|---|
| Date | 2014-01-16 11:52 +0200 |
| Message-ID | <qotk3e0gsoj.fsf@ruuvi.it.helsinki.fi> |
| In reply to | #64067 |
Sam writes:
> I would like to build an array of dictionaries. Most of the
> dictionary example on the net are for single dictionary.
>
> dict = {'a':'a','b':'b','c':'c'}
> dict2 = {'a':'a','b':'b','c':'c'}
> dict3 = {'a':'a','b':'b','c':'c'}
>
> arr = (dict,dict2,dict3)
>
> What is the syntax to access the value of dict3->'a'?
This isn't a special case.
arr[2] to get the dictionary
arr[2]['a'] to get the value in the dictionary
'a' in arr[2] to find if there is such a key
arr[2].get('a') to get the value or None if the key isn't there
arr[2].get('a', 'd') to get a value even if the key isn't there
help(dict.get)
for key in arr[2]:
# to iterate over the keys
The exact same mechanisms are used no matter where you get the
dictionary from.
[toc] | [prev] | [next] | [standalone]
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Date | 2014-01-16 17:01 +0000 |
| Message-ID | <mailman.5593.1389891738.18130.python-list@python.org> |
| In reply to | #64067 |
On 16/01/2014 09:48, Chris Angelico wrote:
> On Thu, Jan 16, 2014 at 8:41 PM, Sam <lightaiyee@gmail.com> wrote:
>> I would like to build an array of dictionaries. Most of the dictionary example on the net are for single dictionary.
>>
>> dict = {'a':'a','b':'b','c':'c'}
>> dict2 = {'a':'a','b':'b','c':'c'}
>> dict3 = {'a':'a','b':'b','c':'c'}
>>
>> arr = (dict,dict2,dict3)
>>
>> What is the syntax to access the value of dict3->'a'?
>
> Technically, that's a tuple of dictionaries
For the benefit of lurkers, newbies or whatever it's the commas that
make the tuple, not the brackets.
--
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.
Mark Lawrence
[toc] | [prev] | [next] | [standalone]
| From | Grant Edwards <invalid@invalid.invalid> |
|---|---|
| Date | 2014-01-17 20:53 +0000 |
| Message-ID | <lbc593$16s$1@reader1.panix.com> |
| In reply to | #64092 |
On 2014-01-16, Mark Lawrence <breamoreboy@yahoo.co.uk> wrote:
> On 16/01/2014 09:48, Chris Angelico wrote:
>> On Thu, Jan 16, 2014 at 8:41 PM, Sam <lightaiyee@gmail.com> wrote:
>>> I would like to build an array of dictionaries. Most of the dictionary example on the net are for single dictionary.
>>>
>>> dict = {'a':'a','b':'b','c':'c'}
>>> dict2 = {'a':'a','b':'b','c':'c'}
>>> dict3 = {'a':'a','b':'b','c':'c'}
>>>
>>> arr = (dict,dict2,dict3)
>>>
>>> What is the syntax to access the value of dict3->'a'?
>>
>> Technically, that's a tuple of dictionaries
>
> For the benefit of lurkers, newbies or whatever it's the commas that
> make the tuple, not the brackets.
In _that_ example, yes. There are other cases where it's the
brackets (sort of):
foo('a','b','c') # three separate string objects are passed
foo(('a','b','c')) # a single tuple object is passed
--
Grant Edwards grant.b.edwards Yow! Being a BALD HERO
at is almost as FESTIVE as a
gmail.com TATTOOED KNOCKWURST.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web