Python's tuple, list, and dictionary data types are very python (there python is a adjective) data structure. These structures are sufficiently optimized, so if used well, there will be great benefits in some area.
Meta-group
Personally, like an array of Java, a tuple in Python has the following characteristics:
- An ordered set of arbitrary objects, this is nothing to say, the array of same-sex;
- Read by offset;
- Once generated, cannot be changed;
- Fixed length, supports nesting
Here's an example:
Python code
- >>> (0, ' haha ', (4j, ' y '))
- (0, ' haha ', (4j, ' y '))
- >>> T = (1, 3, ' B ')
- >>> T[2]
- ' B '
- >>> T[3]
- Traceback (most recent):
- File "#41 >", Line 1, in <module></module>
- T[3]
- Indexerror: tuple index out of range
- >>> T[-1]
- ' B '
- >>> T[0:-1]
- (1, 3)
- >>> T * 2
- (1, 3, ' B ', 1, 3, ' B ')
- >>> for x in t:
- print X,
- 1 3 b
- >>> ' B ' in t
- True
- >>> q = t + ((3, ' abc '))
- >>> Q
- (1, 3, ' B ', 3, ' abc ')
- >>> for x in (2, (3, ' a ')):
- Print x
- 2
- (3, ' a ')
- >>> Len (q)
- 5
- >>> Len ((2, (3, ' abc ' ))
- 2
- >>> (1, 2, 3) [1]
- 2
- >>> q[1] = ' d '
- Traceback (most recent):
- File "#57 >", Line 1, in <module></module>
- Q[1] = ' d '
- TypeError: ' tuple ' object does not support item assignment
- >>> a = (' B ', ' C ', q)
- >>> 1 in a
- False
- >>> Q in a
- True
- >>> A
- (' B ', ' C ', (1, 3, ' B ', 3, ' abc ')
- >>> q= ' d '
- >>> A
- (' B ', ' C ', (1, 3, ' B ', 3, ' abc ')
The above example is sufficient to illustrate that the most important thing when using tuples is "once generated, immutable".
List
The list is like the Java collection, the features are more than the tuple, more flexible, its character summarized as follows:
- An ordered set of arbitrary objects;
- can be accessed by offset, note that the list of elements are variable, which is different from the tuple;
- Variable length, support nesting;
- There are also some Java-like object reference mechanisms
Because of these attributes of the list, which makes the list widely used in practical applications, here are some examples.
1) First, basic usage
Python code
- >>> L = [' A ', ' B ', ' C ']
- >>> len (l)
- 3
- >>> L + [' d ']
- [' A ', ' B ', ' C ', ' d ']
- >>> L * 2
- [' A ', ' B ', ' C ', ' A ', ' B ', ' C ']
- >>> for x in L:
- print X,
- A b C
2) Index and Shard, assignment (single element assignment, Shard Assignment)
Python code
- >>> l = [' abc ', ' def ', ' Ghi ', 123]
- >>> L[2]
- ' Ghi '
- >>> L[-3]
- ' def '
- >>> L[:3]
- [' abc ', ' def ', ' Ghi ']
- >>> l[1] = ' haha '
- >>> L
- [' abc ', ' Haha ', ' ghi ', 123]
- >>> l[1:] = [' Apple ', ' banana ']
- >>> L
- [' abc ', ' Apple ', ' banana ']
- >>> l[2] = [123, 345, 456]
- >>> L
- [' abc ', ' Apple ', [123, 345, 456]
- >>> l[1:] = [123, 234, 345, 456, 567]
- >>> L
- [' ABC ', 123, 234, 345, 456, 567]
3) Add, sort, delete operations
Python code
- >>> l = [' abc ', ' def ', ' Ghi ', 123]
- >>> L. Append (456)
- >>> L
- [' abc ', ' def ', ' Ghi ', 123, 456]
- >>> L.sort ()
- >>> L
- [123, 456, ' abc ', ' def ', ' Ghi ']
- >>> del l[0]
- >>> L
- [456, ' abc ', ' def ', ' Ghi ']
- >>> del l[2:]
- >>> L
- [456, ' ABC ']
4) Some interesting usage (from Forum id-coffee Dancer)
Remove the space from the tail of each element in the list:
Python code () element fixed