Python Advanced (data structure and algorithm [1])

Source: Internet
Author: User
Tags iterable

Decompose a sequence into a separate variable
>>>p = (4,5)# Decompose tuples or sequences by assigning values>>>X, y = P>>>X4>>>Y5>>>data = [' ACME ', -,91.9, ( -,1,1)]>>>Name, shares, prices, date = data>>>Name' ACME '>>>Date -,1,1)>>>Name, shares, prices, (year, month, day) = data>>>Prices91.9>>>Month1>>>Day1>>>String ="Hello" #只要对象是可迭代的, you can perform a decomposition operation>>>A, B, C, D, E = string>>>A' h '>>>B' E '>>>E' O '>>>Name, _, Prices, _ = Data#_表示不感兴趣的项>>>Name' ACME '>>>Prices91.9>>>_( -,1,1)>>>Name, _, Prices, _, outbound= data# unpacking elements do not matchTraceback (most recent): File"<pyshell#38>", line1,inch<module> name, _, Prices, _, outbound= datavalueerror:need more than4Values to unpack
Decompose elements from an arbitrary length of an iterative object
>>>*trailing, current = [ A, at, the, $, About, the, +, the]>>>Current the>>>Trailing# Use "* expression" for decomposition matching[ A, at, the, $, About, the, +]>>>A, B, *, d = [ A, the, $, About, the, the]syntaxerror:invalid syntax>>>A, B, *c, d = [ A, the, $, About, the, the]>>>A A>>>B the>>>c[ $, About, the]>>>D the>>>line =' Nobody:*:-2:user:/home '>>>uname, *others, Path = Line.split (': ')>>>Uname' Nobody '>>>Path' Home '

This is a good fit for an iterative object with a decomposition position or arbitrary length. For fixed components or patterns (for example, after element 2 is a phone number, but the number of phone numbers is unknown), using an asterisk expression can be easily and quickly decomposed.

Save last n elements
#使用collections中的deque实现From collections Import dequed = Deque () d.append (' 1 ') D.append (' 2 ') D.append (' 3 ') Len (d)# 3d[0]# 1d[-1]# 3D = deque (' 12345 ') Len (d)# 5D.popleft ()# 1D.pop ()# 5D# deque ([' 2 ', ' 3 ', ' 4 ')]#我们还可以限制deque的长度:D = deque (maxlen= -)#当限制长度的deque增加超过限制数的项时, the items on the other side are automatically deleted:D = deque (maxlen=2) D.append (1) D.append (2)# deque ([' 1 ', ' 2 '])D.append (3)# deque ([' 2 ', ' 3 '])| Append...) | Add an element to the right side of the deque.   |  | Appendleft (...) | Add an element to the left side of the deque.   |  | Clear...) | Remove all elements from the deque.   |  | Count...) | D.count (value), integer--returnNumber of occurrences of value |  | Extend...) |   Extend the right side of the deque with elements from the Iterable |  | Extendleft (...) |   Extend the left side of the deque with elements from the Iterable |  | Pop...) | Remove andreturnThe rightmost element.   |  | Popleft (...) | Remove andreturnThe leftmost element.   |  | Remove...) | D.remove (value)--Remove first occurrence of value.   |  | Reverse...) |   D.reverse ()--Reverse *in place* |  | Rotate...) | Rotate the deque n steps to the right (default n=1).      If n is negative, | Rotates left.

Python Advanced (data structure and algorithm [1])

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.