Beruflich Dokumente
Kultur Dokumente
Immutable Sequences
Tuples are immutable sequences: they cannot be modified. Tuples and lists have much in common, but lists are mutable sequences: they can be modified. Tuples use parentheses instead of square brackets:
l s t=[ ' a ' ,3 ,0 . 2 ] t u p=( ' a ' ,3 ,0 . 2 )
Optional reading
Once created, items in lists and tuples are accessed using the same notation:
> > >l s t [ 0 ] ' a ' > > >t u p [ 0 ] ' a '
Tuples have fewer methods than lists. In fact, the only regular methods are c o u n tand i n d e x :
> > >d i r ( l i s t ) [ ' _ _ a d d _ _ ' ,' _ _ c l a s s _ _ ' ,' _ _ c o n t a i n s _ _ ' ,' _ _ d e l a t t r _ _ ' ,' _ _ d e l i t e m _ _ ' ,' _ _ d o c _ _ ' ,' _ _ e q _ _ ' ,' _ _ f o r m a t _ _ ' , ' _ _ g e _ _ ' ,' _ _ g e t a t t r i b u t e _ _ ' ,' _ _ g e t i t e m _ _ ' ,' _ _ g t _ _ ' ,' _ _ h a s h _ _ ' ,' _ _ i a d d _ _ ' ,' _ _ i m u l _ _ ' ,' _ _ i n i t _ _ ' , ' _ _ i t e r _ _ ' ,' _ _ l e _ _ ' ,' _ _ l e n _ _ ' ,' _ _ l t _ _ ' ,' _ _ m u l _ _ ' ,' _ _ n e _ _ ' ,' _ _ n e w _ _ ' ,' _ _ r e d u c e _ _ ' ,' _ _ r e d u c e _ e x _ _ ' , ' _ _ r e p r _ _ ' ,' _ _ r e v e r s e d _ _ ' ,' _ _ r m u l _ _ ' ,' _ _ s e t a t t r _ _ ' ,' _ _ s e t i t e m _ _ ' ,' _ _ s i z e o f _ _ ' ,' _ _ s t r _ _ ' , ' _ _ s u b c l a s s h o o k _ _ ' ,' a p p e n d ' ,' c o u n t ' ,e x t e n d ' ,' i n d e x ' ,' i n s e r t ' ,' p o p ' ,' r e m o v e ' ,' r e v e r s e ' ,s o r t ' ] > > >d i r ( t u p l e ) [ ' _ _ a d d _ _ ' ,' _ _ c l a s s _ _ ' ,' _ _ c o n t a i n s _ _ ' ,' _ _ d e l a t t r _ _ ' ,' _ _ d o c _ _ ' ,' _ _ e q _ _ ' ,' _ _ f o r m a t _ _ ' ,' _ _ g e _ _ ' , ' _ _ g e t a t t r i b u t e _ _ ' ,' _ _ g e t i t e m _ _ ' ,' _ _ g e t n e w a r g s _ _ ' ,' _ _ g t _ _ ' ,' _ _ h a s h _ _ ' ,' _ _ i n i t _ _ ' ,' _ _ i t e r _ _ ' , ' _ _ l e _ _ ' ,' _ _ l e n _ _ ' ,' _ _ l t _ _ ' ,' _ _ m u l _ _ ' ,' _ _ n e _ _ ' ,' _ _ n e w _ _ ' ,' _ _ r e d u c e _ _ ' ,' _ _ r e d u c e _ e x _ _ ' , ' _ _ r e p r _ _ ' ,' _ _ r m u l _ _ ' ,' _ _ s e t a t t r _ _ ' ,' _ _ s i z e o f _ _ ' ,' _ _ s t r _ _ ' ,' _ _ s u b c l a s s h o o k _ _ ' ,' c o u n t ' ,' i n d e x ' ]
The rest of the list methods are not available for tuple because they modify the object, and tuples, being immutable, cannot be modified. Af o rcan be used to iterate over the values in a tuple:
> > >t u p=( ' a ' ,3 ,0 . 2 ) > > >f o ri t e mi nt u p : p r i n t ( i t e m ) a 3 0 . 2
Type d i c t
Dictionary
Another way to store collections of data is using Python's dictionary type: d i c t . The general form of a dictionary is:
{ k e y 1 :v a l u e 1 ,k e y 2 :v a l u e 2 ,. . . ,k e y N :v a l u e N }
In the example above, the keys are unique: ' A 1 ' ,' A 2 'and ' A 3 ' . The values are not unique: 8 0 ,9 0and 9 0 .
l e n ( d i c t )
> > >a s n _ t o _ g r a d e={ ' A 1 ' :8 0 ,' A 2 ' :9 0 ,' A 3 ' :9 0 } > > >l e n ( a s n _ t o _ g r a d e ) 3
d e l d i c t [ k e y ]
> > >a s n _ t o _ g r a d e={ ' A 1 ' :8 0 ,' A 2 ' :9 0 ,' A 3 ' :9 0 } > > >d e la s n _ t o _ g r a d e [ ' A 1 ' ] > > >a s n _ t o _ g r a d e { ' A 3 ' :9 0 ,' A 2 ' :9 0 }
If k e ydoes not exist in d i c t , adds k e y and its associated v a l u eto d i c t . d i c t [ k e y ] If k e yexists in =v a l u e d i c t , updates d i c t by setting the value associated with k e y to v a l u e .
> > >a s n _ t o _ g r a d e={ ' A 1 ':8 0 ,' A 2 ' :9 0 ,' A 3 ':9 0 } > > >a s n _ t o _ g r a d e [ ' A 4 ' ]=7 0 > > >a s n _ t o _ g r a d e { ' A 1 ' :8 0 ,' A 3 ' :9 0 ,' A 2 ' :9 0 ,' A 4 ' :7 0 }
The for-loop above printed out the keys of the dictionary. It is also possible to print out the values:
> > >a s n _ t o _ g r a d e={ ' A 1 ' :8 0 ,' A 2 ' :7 0 ,' A 3 ' :9 0 } > > >f o ra s s i g n m e n ti na s n _ t o _ g r a d e : p r i n t ( a s n _ t o _ g r a d e [ a s s i g n m e n t ] ) 8 0 9 0 7 0
Empty Dictionaries
A dictionary can be empty. For example:
d={ }
Heterogeneous Dictionaries
A dictionary can have keys of different types. For example, one key can be of type i n tand another of type s t r :
d={ ' a p p l e ' :1 ,3 :4 }
Immutable Keys
The keys of a dictionary must be immutable. Therefore, lists, dictionary and other mutable types cannot be used as keys. The following results in an error:
d [ [ 1 ,2 ] ]=' b a n a n a '
Since lists are mutable, they cannot be keys. Instead, to use a sequence as a key, type t u p l ecan be used:
d [ ( 1 ,2 ) ]=' b a n a n a '
Inverting a Dictionary
Switching Keys and Values
Dictionaries have keys that are unique and each key has a value associated with it. For example, here is a dictionary mapping fruit to their colours:
f r u i t _ t o _ c o l o u r={ ' w a t e r m e l o n ' :' g r e e n ' ,' p o m e g r a n a t e ' :' r e d ' , ' p e a c h ' :' o r a n g e ' ,' c h e r r y ' :' r e d ' ,' p e a r ' :' g r e e n ' , ' b a n a n a ' :' y e l l o w ' ,' p l u m ' :' p u r p l e ' ,' o r a n g e ' :' o r a n g e ' }
Optional reading
To invert the dictionary, that is, switch the mapping to be colours to fruit, here is one approach:
> > >c o l o u r _ t o _ f r u i t={ } > > >f o rf r u i ti nf r u i t _ t o _ c o l o u r : c o l o u r=f r u i t _ t o _ c o l o u r [ f r u i t ] c o l o u r _ t o _ f r u i t [ c o l o u r ]=f r u i t > > >c o l o u r _ t o _ f r u i t { ' o r a n g e ' :' o r a n g e ' ,' p u r p l e ' :' p l u m ' ,' g r e e n ' :' p e a r ' ,' y e l l o w ' :' b a n a n a ' ,' r e d ' :' p o m e g r a n a t e ' }
The resulting dictionary is missing some fruit. This happens since colours, which are keys, are unique so later assignments using the same colour replace earlier entries. A way to remedy this is to map colours to a list of fruit.
> > >c o l o u r _ t o _ f r u i t { ' o r a n g e ' :[ ' p e a c h ' ,' o r a n g e ' ] ,' p u r p l e ' :[ ' p l u m ' ] ,' g r e e n ' :[ ' w a t e r m e l o n ' ,' p e a r ' ] ,' y e l l o w ' :[ ' b a n a n a ' ] ,' r e d ' :[ ' c h e r r y ' ,' p o m e g r a n a t e ' ] }