List of data structures in Python

I'm new to Python (coming from the Java background), so it was interesting if anyone had any advice on the structure of the data structure. I need to create a data structure with default values ​​that would look something like this:

[
(Name="name1",  {id=1, val1="val1"} ),
(Name="name2",  {id=2, val1="val2"} )
]

ie a list of tuples, where each tuple consists of one string value (Name) and a dictionary of values.

The first part of the functionality that I need is the ability to add or override the above data structure with additional data, for example:

[
(Name="name2",  {id=2, val1="new value"} )  ,
(Name="name2",  {id=3, val1="another value"} )  ,
(Name="name3",  {id=3, val1="val3"} )
]

Which ultimately leads to the final data structure, which looks like this:

[
(Name="name1",  {id=1, val1="val1"} ),
(Name="name2",  {id=2, val1="new value"} )  ,
(Name="name2",  {id=3, val1="another value"} )  ,
(Name="name3",  {id=3, val1="val3"} )
]

The second part of the functionality I need is to have access to each tuple in the list according to the id value in the ie dictionary

, name = "name2" id = "3".

- , Python?

!

+4
2

namedtuple , , , , , .

+1

.

, Python, . ( ) .

Python -. :

dict = {'Alice': '2341', 'Beth': '9102', 'Cecil': '3258'}

:

dict1 = { 'abc': 456 };
dict2 = { 'abc': 123, 98.6: 37 };

(:), , . - , : {}.

, . , , , .

:

, , . :

#!/usr/bin/python

dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'};

print "dict['Name']: ", dict['Name'];
print "dict['Age']: ", dict['Age'];
When the above code is executed, it produces the following result:

dict['Name']:  Zara
dict['Age']:  7

: http://www.tutorialspoint.com/python/python_dictionary.htm

0

Source: https://habr.com/ru/post/1545387/


All Articles