Žodynai

Tas pats, kaip ir assoc. masyvas.

Unlike sequences, which are indexed by a range of numbers, 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 in place using index assignments, slice assignments, or methods like append() and extend().

Žodyno pavyzdys

asmenys = { 'Jonas' : 'Jonaitis', 'Petras' : 'Petraitis', 'Vardenis' : 'Pavardenis' }
 
# Kokia Jono pavarde?
print asmenys['Jonas']
 
# Kokius vardus zhinome?
print asmenys.keys ()
 
# Du budai patikrinti, ar yra irashyta Petro pavarde
if (asmenys.has_key ('Petras')):
	print "Petras yra"
 
if ('Petras' in asmenys):
	print "Petras yra"

Žodyno darymas iš sąrašo/tuple'o

# Su dict() pasidarome zhodyna ish sarashu/tuples'u
print dict([('sape', 4139), ('guido', 4127), ('jack', 4098)]) # {'sape': 4139, 'jack': 4098, 'guido': 4127}
print dict([(x, x**2) for x in (2, 4, 6)]) # {2: 4, 4: 16, 6: 36}
This website uses cookies for visitor traffic analysis. By using the website, you agree with storing the cookies on your computer.More information
 
Jei nenurodyta kitaip, šio wiki turinys ginamas tokia licencija: CC Attribution-Noncommercial-Share Alike 4.0 International
Recent changes RSS feed Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki