Python (data structure and algorithm [1]) and python (advanced tutorial)

Source: Internet
Author: User

Python (data structure and algorithm [1]) and python (advanced tutorial)
Splits a sequence into individual variables.

>>> P = () # Break Down tuples or sequences by assigning values >>> x, y = p >>>> x4 >>> y5 >>> data = ['acme ', 50, 91.9, (, 1)] >>>> name, shares, prices, date = data >>> name 'acme '>>> date (2000, 1, 1) >>> name, shares, prices, (year, month, day) = data >>> prices91.9 >>> month1 >>>day1 >>> string = "hello" # As long as the object is iterated, you can perform the decomposition operation >>>, b, c, d, e = string >>> a'h' >>> B 'e' >>> e'o' >>>> name, _, prices, _ = data # _ items that are not interest >>>> name 'acme '>>> prices91.9 >>>_( 2000, 1, 1) >>>> name ,_, prices, _, outbound = data # The unwrapped element does not match Traceback (most recent call last): File "<pyshell #38>", line 1, in <module> name ,_, prices, _, outbound = dataValueError: need more than 4 values to unpack
Break down elements from iteratable objects of any length
>>> * Trailing, current = [12, 23, 34, 45, 56, 67, 78, 89] >>> current89 >>> trailing # use the "* expression" for decomposition and matching [12, 23, 34, 45, 56, 67, 78] >>>, b, *, d = [12, 34, 45, 56, 67,89] SyntaxError: invalid syntax> a, B, * c, d = [12, 34, 45, 56, 67,89] >>> a12 >>> b34 >>> c [45, 56, 67] >>>> d89 >>> line = 'Nobody: *:-2: user:/home '>>> uname, * others, path = line. split (':') >>> uname 'nobody' >>> path '/home'

It is no longer appropriate for iteratable objects at the decomposition position or any length. For fixed components or modes (for example, after element 2 is a phone number, but the number of phone numbers is unknown), the asterisk expression can be used for convenient and quick decomposition.

Save the last N elements
# Use deque in collections to implement from collections import dequed = deque () d. append ('1') d. append ('2') d. append ('3') len (d) # 3d [0] # 1d [-1] # 3d = deque ('000000') len (d) # 5d. popleft () # 1d. pop () # 5d # deque (['2', '3', '4']) # We can also limit the length of deque: d = deque (maxlen = 30) # When a deque with a maximum length exceeds the limit, 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 -- return number 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 and return the rightmost element. | popleft (...) | Remove and return the 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.

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.