"Learn Python with me." Python data type

Source: Internet
Author: User

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
  1. >>> (0, ' haha ', (4j, ' y '))
  2. (0, ' haha ', (4j, ' y '))
  3. >>> T = (1, 3, ' B ')
  4. >>> T[2]
  5. ' B '
  6. >>> T[3]
  7. Traceback (most recent):
  8. File "#41 >", Line 1, in <module></module>
  9. T[3]
  10. Indexerror: tuple index out of range
  11. >>> T[-1]
  12. ' B '
  13. >>> T[0:-1]
  14. (1, 3)
  15. >>> T * 2
  16. (1, 3, ' B ', 1, 3, ' B ')
  17. >>> for x in t:
  18. print X,
  19. 1 3 b
  20. >>> ' B ' in t
  21. True
  22. >>> q = t + ((3, ' abc '))
  23. >>> Q
  24. (1, 3, ' B ', 3, ' abc ')
  25. >>> for x in (2, (3, ' a ')):
  26. Print x
  27. 2
  28. (3, ' a ')
  29. >>> Len (q)
  30. 5
  31. >>> Len ((2, (3, ' abc ' ))
  32. 2
  33. >>> (1, 2, 3) [1]
  34. 2
  35. >>> q[1] = ' d '
  36. Traceback (most recent):
  37. File "#57 >", Line 1, in <module></module>
  38. Q[1] = ' d '
  39. TypeError: ' tuple ' object does not support item assignment
  40. >>> a = (' B ', ' C ', q)
  41. >>> 1 in a
  42. False
  43. >>> Q in a
  44. True
  45. >>> A
  46. (' B ', ' C ', (1, 3, ' B ', 3, ' abc ')
  47. >>> q= ' d '
  48. >>> A
  49. (' 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
    1. >>> L = [' A ', ' B ', ' C ']
    2. >>> len (l)
    3. 3
    4. >>> L + [' d ']
    5. [' A ', ' B ', ' C ', ' d ']
    6. >>> L * 2
    7. [' A ', ' B ', ' C ', ' A ', ' B ', ' C ']
    8. >>> for x in L:
    9. print X,
    10. A b C

2) Index and Shard, assignment (single element assignment, Shard Assignment)

Python code
  1. >>> l = [' abc ', ' def ', ' Ghi ', 123]
  2. >>> L[2]
  3. ' Ghi '
  4. >>> L[-3]
  5. ' def '
  6. >>> L[:3]
  7. [' abc ', ' def ', ' Ghi ']
  8. >>> l[1] = ' haha '
  9. >>> L
  10. [' abc ', ' Haha ', ' ghi ', 123]
  11. >>> l[1:] = [' Apple ', ' banana ']
  12. >>> L
  13. [' abc ', ' Apple ', ' banana ']
  14. >>> l[2] = [123, 345, 456]
  15. >>> L
  16. [' abc ', ' Apple ', [123, 345, 456]
  17. >>> l[1:] = [123, 234, 345, 456, 567]
  18. >>> L
  19. [' ABC ', 123, 234, 345, 456, 567]

3) Add, sort, delete operations

Python code
  1. >>> l = [' abc ', ' def ', ' Ghi ', 123]
  2. >>> L. Append (456)
  3. >>> L
  4. [' abc ', ' def ', ' Ghi ', 123, 456]
  5. >>> L.sort ()
  6. >>> L
  7. [123, 456, ' abc ', ' def ', ' Ghi ']
  8. >>> del l[0]
  9. >>> L
  10. [456, ' abc ', ' def ', ' Ghi ']
  11. >>> del l[2:]
  12. >>> L
  13. [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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.