Created
December 11, 2011 20:10
-
-
Save dketov/1462480 to your computer and use it in GitHub Desktop.
Словарь
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Определение | |
""" | |
d = {"A":"a", "B":"b"} | |
print d | |
print d["A"] | |
print d["B"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Составные типы данных | |
""" | |
mel = {'name': 'Mark', | |
'jobs': ['trainer', 'writer'], | |
'web': 'www.rmi.net/~lutz', | |
'home': {'state': 'CO', 'zip':80501}} | |
print mel['name'] | |
print mel['jobs'] | |
print mel['jobs'][1] | |
print mel['home']['zip'] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Индексы и операции со словарём | |
""" | |
# Dictionaries are indexed by keys, which can be any immutable type; | |
# strings and numbers can always be keys. | |
# Tuples can be used as keys if they contain only strings, numbers, or tuples; | |
# if a tuple contains any mutable object either directly or indirectly, | |
# it cannot be used as a key. | |
# You can't use lists as keys, since lists can be modified. | |
# a dictionary is an unordered set of key: value pairs, with the requirement that the | |
# keys are unique (within one dictionary). | |
tel = {'jack': 4098, 'sape': 4139} | |
tel['guido'] = 4127 | |
print tel | |
print tel['jack'] | |
del tel['sape'] | |
tel['irv'] = 4127 | |
print tel | |
tel.keys() | |
print tel.has_key('guido') | |
print 'guido' in tel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Конструирование словаря из списков | |
""" | |
# dict() constructor builds dictionaries directly from lists of key-value pairs stored | |
# as tuples. | |
print dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) | |
print dict([(x, x**2) for x in (2, 4, 6)]) # use a list comprehension | |
# When the keys are simple strings, it is sometimes easier to specify pairs using | |
# keyword arguments: | |
print dict(sape=4139, guido=4127, jack=4098) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Доступ к элементам словаря | |
""" | |
params = {"server":"mpilgrim", "database":"master", "uid":"sa", "pwd":"secret"} | |
print params.keys() | |
print params.values() | |
print params.items() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Поиск ключа | |
""" | |
d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionary | |
print d2 # order is scrambled | |
print d2.has_key('ham') # key membership test (1 means true) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Получение значения | |
""" | |
d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionary | |
print d2['spam'] # fetch value by key |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Изменение, добавление и удаление значений | |
""" | |
d2 = {'spam': 2, 'ham': 1, 'eggs': 3} # make a dictionary | |
print d2 # order is scrambled | |
d2['ham'] = ['grill', 'bake', 'fry'] # change entry | |
del d2['eggs'] # delete entry | |
d2['brunch'] = 'Bacon' # add new entry | |
print d2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Копирование словаря | |
""" | |
x = {'username': 'admin', 'machines': ['foo', 'bar', 'baz']} | |
y = x.copy() | |
y['username'] = 'mlh' | |
y['machines'].remove('bar') | |
print y | |
print x |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Вывод значений словаря | |
""" | |
print "%(n)d %(x)s" % {"n":1, "x":"spam"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment