Python Sequence Container

Source: Internet
Author: User

Sequence in Python mainly contains sequences that store a sequence of individual elements and two pairs of elements, and Str is a character container.

The single-element sequence has the following main types:

ByteArray: Byte array, created by built-in function ByteArray ()Xrange: Created by the function xrange (n)/xrange (start, stop, step), but does not support slicing on the container, connection, copy, in/not in judgment, Min/max value operation, Xrange isimmutable sequence, xrange object size is fixed, it is independent of the size of the range, Xrange is mainly used for circulation, slightly more efficient than range, the operation on Xrange only supportsindexing, iteration, Len.List: Linked list mutable, [A,b,c], equivalent to the collection of array lists in other languages, so also for partial linklist and arraylisttuple:immutable list, non-empty tuple brackets are optional a,b,c or (A,B,C) , empty tuple must have parentheses (), single item A tuple comma is necessary a, or (a,); Cannot assign a value to an element of a tuple, but the element in a tuple can be mutableset:mutable unordered Hasht Able, support for mathematical operations and set sets, etc., because unordered, so does not support indexing,slicing and other sequential operations flight {a,b,c} empty set (), Support issubset/union/intersection/ Differencefrozenset:immutable unordered Hashtable

The two-element sequence is primarily an associative array:

dict:hash_table, comma-separated List of Key-value: {a:b, c:d}, Empty Dic:dic (), mainly in the following ways:ITER (d) is short for Iterkeys ()Get (Key)/has_key (key)/pop (key)/popitem ()Items (): Return a copy of the dictionary ' s list of (key, value) pairsKeys (): Return a list of keysSorted (keys ()):return a sorted list of keysDictview:dict.viewkeys ()/viewvalues ()/viewitems () returns, is the view of Dict, that is, the dict changes, the view will change
Sequence main operation is to delete and change, the following check operation priority is incremented, S, T type is the same, N, I, J is plastic
Operation Result Notes
x in s true  if an item of   s  is equal to  x , else false (1)
x not in s false  if an item of   s  is equal to  x , else true (1)
S + t The concatenation of s and t (6)
S * N, N * s n shallow copies of s concatenated (2)
S[i] ith item of s, Origin 0 (3)
S[I:J] Slice of s from i to J (3) (4)
S[I:J:K] Slice of s from i to J with step k (3) (5)
Len (s) Length of s
Min (s) Smallest item of s
Max (s) Largest item of s
S.index (x) Index of the first occurrence of x in s
S.count (x) Total number of occurrences of x in s
Comparisons can also be made between containers, comparing each element in a container in turnIf S is Str/unicode, in/not in determines if X is a string of sN<0 S*n = s*0 Returns an empty container with the same type as SIf i/j < 0, s[i/j] 's position is Len (s) + i/j, but s[0]=s[-0]I,j can be omitted, omit I i=0, omit J J=len (s), if I/j>len (s), then take Len (s), if i>=j, then slice return emptySequence can be divided into mutable and immutable according to variability:Immutable sequence:string, xrange, tuple, once created, can no longer be changedmutable Sequence:bytearray, list can add elements or change the elements of a location, the variable container supports the following operations
operation result notes
s[i] = x ITEM&NBSP; i  of  s  is replaced by  x &NBSP;
s[i:j] = t slice of  s  from  i  to  J  is replaced by the contents of the iterable  t &NBSP;
del s[i:j" same as s[i:j] = [" &NBSP;
s[i:j:k] = t the elements of s[i:j:k"  are replaced by those of  t (1)
del s[i:j:k" removes The Elements of s[i:j:k"  from the list &NBSP;
s.append (x) same as s[len (s): Len (s)] = [x] (2)
s.extend (x) same as s[len (s): Len (s)] = x (3)
s.count (x) return number of  i ' for which s[i] == x &NBSP;
s.index (X[, i[, j]) return smallest  k  such that s[k] == x  and i <= k < j (4)
s.insert (i, x) same as s[i:i] = [x" (5)
s.pop ([i]) same as x = s[i]; del s[i]; return x (6)
S.remove (x) Same as del S[s.index (x)] (4)
S.reverse () Reverses the items of s in place (7)
S.sort ([cmp[, key[, reverse]]) Sort the items of s in place (7) (8) (9) (10)
The length of t must be equal to the length of sliceX can be any iterable type
Sequence Traversal:For x in S: ... Enumerate (): RetrieveThe position index and corresponding value at the same timezip ():loop over, or more sequences at the same timeIteritems (): RetrieveThe key and corresponding value at the same time whileLooping through dictionariesIf you need to modify the collection when iterating through the collection, it is best to iterate through the copy of the collection, slice[:] It is easy to get the copy of the collection

Python Sequence Container

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.