Python Simple data structure (i)

Source: Internet
Author: User
Tags dedupe

Study Notes

#python 3.5.2


Extract sequence assignment to multiple variables

>>> x= (a,b,c=x>>>) >>> a1>>> b2>>> c3>>> data=[' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ']>>> _,a,_,_,b=data>>> a ' 2 ' >>> B ' 5 ' >>> y= (3,4) >>> a,b,c=  Ytraceback (most recent): File "<pyshell#14>", line 1, <module> a,b,c=yvalueerror:not enough Values to unpack (expected 3, got 2) >>> >>> data=[' 1 ', 2, (3,4),5]>>> a,b,l,c=data>>> L (3, 4) >>> a ' 1 ' >>> c5>>>

Unzip an iterative object to assign values to multiple variables

>>> l=[1,2,3,4,5,6]>>> head,*tail=l>>> head1>>> tail[2, 3, 4, 5, 6]>>> Head,*middle,tail=l>>> head1>>> middle[2, 3, 4, 5]>>> tail6>>> L=[' aaa ', 10, (all ), ' dddd ']>>> head,*middle,tail=l>>> head ' AAA ' >>> middle[10, (1, 2, 3)]>>> tail ' dddd ' >>> name,age, (a,b,c),addr=l>>> a1>>> b2>>> c3>>> addr ' dddd ' > >> age10>>>


Keep Last n elements

>>> from Collections Import deque>>> Q=deque (maxlen=4) >>> q.append (1) >>> Q.append (2) >>> q.append (3) >>> qdeque ([1, 2, 3], maxlen=4) >>> q.append (4) >>> qdeque ([1, 2, 3 , 4], maxlen=4) >>> q.append (5) >>> Qdeque ([2, 3, 4, 5], maxlen=4) >>> q.append (6) >>> Qdeque ([3, 4, 5, 6], maxlen=4) >>> q.appendleft (0) >>> Qdeque ([0, 3, 4, 5], maxlen=4) >>> Q.appen Dleft ( -1) >>> Qdeque ([-1, 0, 3, 4], maxlen=4) >>>


Finds the largest or smallest n elements

>>> import Heapq>>> nums = [1, 8, 2, 23°c, 7, -4,,, 2]>>> heapq.nlargest (3,nums) [42 , Notoginseng, 23]>>> heapq.nlargest (4,nums) [All, Panax Notoginseng, 23]>>> heapq.nsmallest (2,nums) [ -4, 1]>>> Heapq.nsmallest (4,nums) [ -4, 1, 2, 2]>>>


Implementing a priority queue

>>> Import heapq>>> queue=[]>>> heapq.heappush (Queue, (3, ' three ')) >>> Heapq.heappush (Queue, (2, ')) >>> Heapq.heappush (Queue, (5, ' five ')) >>> Heapq.heappush (queue, (1 , ' one ') >>> Heapq.heappush (Queue, (4, ' four ')) >>> Heapq.heappop (queue) (1, ' one ') >>> Heapq.heappop (Queue) (2, ' both ') >>> Heapq.heappop (Queue) (3, ' three ') >>> Heapq.heappop (Queue) (4, ' Four ') >>> Heapq.heappop (queue) (5, ' five ') >>>



Dictionary middle key Mapping multiple values

>>> from Collections Import defaultdict>>> d=defaultdict (list) >>> d[' A '].append (1) > >> d[' A '].append (2) >>> d[' A '].append (3) >>> d[' B '].append ( -1) >>> d[' B '].append (-2) >>> d[' C '].append (' C ') >>> ddefaultdict (<class ' list '), {' B ': [-1,-2], ' C ': [' C '], ' a ': [1, 2, 3]} ) >>>



Dictionary hold Order

>>> from Collections Import ordereddict>>> d=ordereddict () >>> d[' B ']=2>>> d[' A ']= 1>>> d[' C ']=3>>> d[' F ']=6>>> d[' E ']=5>>> dordereddict ([' B ', 2), (' A ', 1), (' C ', 3) , (' F ', 6), (' E ', 5)]) >>> for I in D:print (I,d[i]) B 2A 1C 3F 6E 5>>> d=dict () >>> d[' B '] =2>>> d[' A ']=1>>> d[' C ']=3>>> d[' F ']=6>>> d[' E ']=5>>> for I in D:prin T (I,d[i]) A 1F 6C 3B 2E 5>>>



The operation of the dictionary

>>> prices = {     ' ACME ': 45.23,     ' AAPL ': 612.78,     ' IBM ': 205.55,     ' HPQ ': 37.20,      ' FB ':  10.75}>>> min_price=min (Zip (prices.values (), Prices.keys ())) >> > min_price (10.75,  ' FB ') >>> max_price=max (Zip (prices.values (), Prices.keys ())) > >> max_price (612.78,  ' AAPL ') >>> prices_sorted=sorted (Zip (prices.values (), Prices.keys ())) >>> prices_sorted[(10.75,  ' FB '),  (37.2,  ' HPQ '),  (45.23,  ' ACME '),  (205.55,  ' IBM '),  (612.78,  ' AAPL ')]>>> min (prices) ' AAPL ' >>>  max (Prices) ' IBM ' >>> min (prices,key=lambda x:prices[x]) ' FB ' >>> max ( Prices,key=lambda x:prices[x]) ' AAPL ' >>> prices[min (Prices,key=lambda x:prices[x]) 10.75>>> prices[maX (Prices,key=lambda x:prices[x])]612.78>>> 



Find the same point for two dictionaries

>>> a = {' X ': 1, ' Y ': 2, ' z ': 3}>>> b = {' W ': Ten, ' X ': one, ' y ': 2}>>> A.keys () & B.keys () {' x ', ' Y '}>>> a.keys ()-B.keys () {' Z '}>>> a.items () & B.items () {(' Y ', 2)}&G T;>>


Delete sequence of same elements and maintain order

>>> def dedupe (items): Seen =set () for item in Items:if item not in Seen:yield Itemseen.add (item) >>> a = [ 1, 5, 2, 1, 9, 1, 5, 10]>>> list (Dedupe (a)) [1, 5, 2, 9, 10]

>>>



Named slices

>>> record = ' ......... ..... 100 ..... 513.25 ... ' >>> shares=slice (20,23) >>> price=slice (31,37) >>> Cost=int (record[ Shares]) *float (Record[price]) >>> cost51325.0>>>



This article is from the "Chauncey" blog, make sure to keep this source http://cqwujiang.blog.51cto.com/10808946/1948892

Python Simple data structure (i)

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.