Sie sind auf Seite 1von 6

Tuples

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 )

Chapter 11.2 Storing Data Using Tuples

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 '

Slicing can be used with both:


> > >l s t [ : 2 ] [ ' a ' ,3 ] > > >t u p [ : 2 ] ( ' a ' ,3 )

Tuples cannot be modified:


> > >t u p [ 0 ]=' b ' T y p e E r r o r :' t u p l e 'o b j e c td o e sn o ts u p p o r ti t e ma s s i g n m e n t

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

A tuple can be passed as an argument to the built-in function l e n :


> > >l e n ( t u p ) 3

It is also possible to iterate over the indices of a tuple:


> > >f o rii nr a n g e ( l e n ( t u p ) ) : p r i n t ( t u p [ i ] ) a 3 0 . 2

Jennifer Campbell Paul Gries University of Toronto

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 }

Chapter 11.3 Storing Data Using Dictionaries Optional reading

Keys must be unique. Values may be duplicated. For example:


a s n _ t o _ g r a d e={ ' A 1 ' :8 0 ,' A 2 ' :9 0 ,' A 3 ' :9 0 }

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 .

How To Modify Dictionaries


Dictionaries are mutable: they can be modified. There are a series of operations and methods you can apply to dictionaries which are outlined below. Operation Description Example
> > >a s n _ t o _ g r a d e={ ' A 1 ' :8 0 ,' A 2 ' :9 0 ,' A 3 ' :9 0 } > > >' A 1 'i na s n _ t o _ g r a d e T r u e > > >8 0i na s n _ t o _ g r a d e F a l s e

Checks whether o b j e c ti n o b j e c tis a key in d i c t d i c t .

l e n ( d i c t )

Returns the number of keys in 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 ]

Removes a key and its associated value from 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 } > > >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 }

Accessing Information From Dictionaries


Dictionaries are unordered. That is, the order the key-value pairs are added to the dictionary has no effect on the order in which they are accessed. For example:
> > >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 s i g n m e n t ) A 1 A 3 A 2

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

Finally, both the keys are values can be printed:


> > >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 s i g n m e n t ,a s n _ t o _ g r a d e [ a s s i g n m e n t ] ) A 18 0 A 39 0 A 27 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 '

Jennifer Campbell Paul Gries University of Toronto

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 ' }

Chapter 11.4 Inverting a Dictionary

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.

Mapping A Key To A List


For the example above, we need to consider two cases when adding a colour and a fruit to the dictionary: 1. If the colour is not a key in the dictionary, add it with its value being a single element a list consisting of the fruit. 2. If the colour is already a key, append the fruit to the list of fruit associated with that key.
> > >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 : #W h a tc o l o u ri st h ef r u i t ? c o l o u r=f r u i t _ t o _ c o l o u r [ f r u i t ] i fn o t( c o l o u ri nc o l o u r _ t o _ 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 ] e l s e : c o l o u r _ t o _ f r u i t [ c o l o u r ] . a p p e n d ( f r u i t )

> > >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 ' ] }

Jennifer Campbell Paul Gries University of Toronto

Das könnte Ihnen auch gefallen