pythoncookbook--Data Structures and algorithms

Source: Internet
Author: User
Tags dedupe
The first chapter data structure and algorithm

1.1 Splitting a sequence into separate variables

p = (4, 5) x, y = pprint x print y data = [' ACME ', ' 91.1 ', ' (') ', ' + ', ' + ')]name, shares, price, date = Dataprint Namepri NT shares print price print date name, shares, Price, (year, Mon, day) = Dataprint Year p = (4, 5) #x, y, z = p error!!! s = ' hello! ' A, B, C, D, E, F = sprint aprint Fdata = [' ACME ', ' 91.1 ', ' (') ', ']_ ', ' shares ', ' price ', ' = ' data print Sharesprint ' price# other data can be discarded.

1.2 Explode elements from an arbitrary length of an iterative object

From Audioop import Avgdef Drop_first_last (Grades): First, *middle, last = grades return avg (middle) record = (' Dave ' , ' dave@example.com ', ' 777-333-2323 ', ' 234-234-2345 ') name, email, *phone_numbers = recordprint name Print Emailprint Phon e_numbers*trailing, current = [Ten, 8, 7, 2, 5]print trailing #[10, 8, 7, 2,]print current #5records = [(' foo ') , 1, 2), (' Bar ', ' Hello '), (' foo ', 5, 3)]def Do_foo (x, y): print (' foo ', X, y) def Do_bar (s): Print (' Bar ', s) for tag, *args in records:if tag = = ' Foo ': Do_foo (*args) elif tag = = ' Bar ': D O_bar (*args) line = ' Asdf:fedfr234://wef:678d:asdf ' uname, *fields, homedir, sh = line.split (': ') print uname print h Omedirrecord = (' ACME ', ' 10, ' 123.45, () ', ' *_, ' (*_, year) = Recordprint Nameprint Yearitems = [1, 7, 4, 5, 9]head, *tail = Itemsprint head #1print tail #[10, 7, 4, 5, 9]def sum (items): Head, *tail = items return head + S Um (tail) if tail else heaDSum (items) 

1.3 Save last n elements

From _collections import Dequedef search (lines, pattern, history=5):    previous_lines = deque (MaxLen = history)        for the lines: if pattern in line:            yield line, previous_lines        previous_lines.append (line) # Example a fileif __name__ = = ' __main__ ': With    open (' Somefile.txt ') as F:        for line, prevlines in search (f, ' Python ', 5): 
  for pline in Prevlines: print (                pline) #print (Pline, end= ") print (line            ) #print (Pline, end=")            print ('- ' *20)            q = deque (maxlen=3) q.append (1) q.append (2) q.append (3) Print qq.append (4) Print QQ = Deque () q.append (1) Q.append (2) q.append (3) Print qq.appendleft (4) Print Qq_pop = Q.pop () print Q_popprint qq_popleft = Q.popleft () print q_ Popleftprint Q

1.4 Finding the largest or smallest n elements

Import heapqnums = [1,30,6,2,36,33,46,3,23,43]print (Heapq.nlargest (3, Nums)) print (Heapq.nsmallest (3, Nums)) Portfolio = [                 {' name ': ' IBM ', ' shares ': +, ' price ': 2.4},                 {' name ': ' A ', ' shares ': 1040, ' Price ': 12.4},                 {' Name ': ' S ', ' shares ': 23.4},                 {' name ': ' D ', ' shares ': 1, ' Price ': 2.49},                 {' name ': ' F ', ' shares ': 9, ' Price ': $ '             ]cheap = heapq.nsmallest (3, Portfolio, Key=lambda s:s[' price ') expensive = Heapq.nlargest (3, Portfolio, Key=lambda s:s[' price ') print cheapprint expensivenums = [1,8,2,23,7,-4,18,23,42,37,2]heap = List (nums) print Heapheapq.heapify (heap) print Heapprint heapq.heappop (heap) print heapq.heappop (heap) print heapq.heappop (heap)

1.5 Implementing a priority queue

Import Heapqclass priorityqueue:    def __init__ (self):        self._queue = []        self._index = 0    def push (self, Item, Priority):        Heapq.heappush (Self._queue, (-priority, Self._index, item))        Self._index + = 1    def pop ( Self):        return Heapq.heappop (Self._queue) [-1] #Exampleclass Item:    def __init__ (self, name):        self.name = Name    def __repr__ (self):        return ' Item ({!r}) '. Format (self.name) q = Priorityqueue () q.push (Item (' foo '), 1) Q.push (item (' Spam '), 4) Q.push (item (' Bar '), 5) Q.push (item (' Grok '), 1) print q.pop () print q.pop () print q.pop () a = Item ('  Foo ') b = Item (' bar ') #a < b    Errora = (1, item (' foo ')) b = (5, item (' Bar ')) print a < BC = (1, Item (' Grok ')) #a < C  Errora = (1, 0, item (' foo ')) b = (5, 1, item (' Bar ')) c = (1, 2, item (' Grok ')) print a < Bprint a < C

1.6 Mapping to multiple values in the dictionary

D = {          ' a ': [1, 2, 3],          ' B ': [4, 5]       }  e = {          ' a ': {1, 2, 3},          ' B ': {4, 5}       } from    Collectio NS Import defaultdict    d = defaultdict (list)  d[' A '].append (1)  d[' A '].append (2)  d[' A '].append (3)  print D    = defaultdict (set)  d[' A '].add (1)  d[' A '].add (2)  d[' A '].add (3)  print D    d = {}  d.setdefault (' A ', []). Append (1)  d.setdefault (' A ', []). Append (2)  d.setdefault (' B ', []). Append (3)  print d     d = {}  for key, value in D: #pairs:      if key not in D:          d[key] = []      D[key].app End (value)    d = defaultdict (list)  for key, value in D: #pairs:      d[key].append (value)

1.7 Keep the dictionary in order

From collections import Ordereddict    d = ordereddict ()  d[' foo '] = 1  d[' bar '] = 2  d[' spam '] = 3  d[ ' Grol ' = 4  for key in D:      print (key, D[key])        import JSON    json.dumps (d)

1.8 Dictionary-related computational problems

Price = {' ACME ': 23.45, ' IBM ': 25.45, ' FB ': 13.45, ' IO ': 4.45, ' JAVA ': 45.45, ' AV ': 38.38,} min_price = min (Zip (price.values (), Price.keys ())) Print min _price max_price = max (Zip (price.values (), Price.keys ())) print Max_price price_sorted = sorted (Zip (price.val UEs (), Price.keys ())) print price_sorted price_and_names = Zip (Price.values (), Price.keys ()) Print (min (price_ And_names)) #print (Max (price_and_names)) error zip () creates an iterator that can only be consumed once print min (price) print max (price) print mi N (price.values ()) Print Max (Price.values ()) print min (price, key = Lambda k:price[k]) print max (price, key = Lamb              Da k:price[k]) min_value = price[min (price, key = Lambda k:price[k]) [Print min_value price = { ' AAA ': $, ' ZZZ ': All,} print min (Zip (price.values (), Price.keys ())) Print max (Zip (Price.va Lues (), Price.keys ())) 

1.9 Finding the same points in two dictionaries

A = {          ' x ': 1,          ' y ': 2,          ' Z ': 3       }  B = {          ' x ': one,          ' y ': 2,          ' W ':       }    print A.keys () & B.keys () #{' x ', ' y '}  print A.keys ()-B.keys () #{' z '}  print A.items () & B.items () #{(' Y ', 2)}    C = {Key:a[key] for key in A.keys ()-{' Z ', ' W '}}  Print c #{' x ': 1, ' Y ': 2}

1.10 Removing duplicates from a sequence and keeping the order between elements unchanged

def dedupe (items):      seen = set () for      item in items:          If Item is not in seen:              yield item              seen.add (item) 
  
    #example  a = [1,5,2,1,9,1,5,10]  Print List (Dedupe (a))    def dedupe2 (items, key = None):      seen = set () For item in the      items:          val = Item if key is None else key (item)          If Val isn't in seen:              yield item              seen. Add (val)   #example  a = [           {' X ': 1, ' Y ': 2},           {' x ': 1, ' Y ': 3},           {' x ': 1, ' Y ': 2},           {' X ': 2, ' Y ': 4} ,        ] Print  list (Dedupe2 (A, Key=lambda D: (d[' x '], d[' y ')))  Print List (Dedupe2 (A, Key=lambda D: (d[' x ') ])))    a = [1,5,2,1,9,1,5,10]  print set (a)
  

1.11 To name a slice

Items = [0,1,2,3,4,5,6]  a = slice (2,4)  print Items[2:4]  print items[a]  items[a] = [10,11]  Print Items    Print A.start  print a.stop  print A.step

1.12 Finding the most frequently occurring elements in a sequence

words = [              ' Look ', ' in ', ' my ', ' Eyes ', ' look ', ' in ', ' my ', ' Eyes ',              ' the ', ' look '           ] from    collections I Mport Counter    word_counts = Counter (words)  Top_three = Word_counts.most_common (3)  print Top_three    print word_counts[' look ']  print word_counts[' the '    morewords = [' Why ', ' is ', ' you ', ' not ', ' looking ', ' In ', ' my ', ' Eyes ') for  word in morewords:      Word_counts[word] + = 1  print word_counts[' Eyes ']  print word_counts[' why ']    word_counts.update (morewords)  print word_counts[' Eyes ']  print word_counts['  Why ']    a = Counter (words)  B = Counter (morewords)  print a  print b  c = a + b  print C  d = a -B  Print B

1.13 Sort the dictionary list by public key

rows = [              {' fname ': ' Brian ', ' lname ': ' Jones ', ' uid ': 1003},              {' fname ': ' David ', ' lname ': ' Beazley ', ' uid ': 1002},              {' fname ': ' John ', ' lname ': ' Cleese ', ' UID ': 1001},              {' fname ': ' Big ', ' lname ': ' Jones ', ' uid ': 1004}          ]    From operator import Itemgetter    rows_by_fname = sorted (rows, Key=itemgetter (' fname '))  Rows_by_uid = sorted ( Rows, Key=itemgetter (' uid '))  print rows_by_fname  print rows_by_uid  rows_by_lfname = sorted (rows, key= Itemgetter (' lname ', ' fname '))  print rows_by_lfname    rows_by_fname = sorted (rows, Key=lambda r:r[' fname ')  rows_by_lfname = sorted (rows, Key=lambda r: (r[' fname '), r[' lname ']))  print rows_by_fname  print Rows_ By_lfname    Print min (Rows, key=itemgetter (' uid '))  print max (rows, key=itemgetter (' uid '))

1.14 Sorting objects that do not natively support comparison operations

Class User:      def __init__ (self, user_id):          self.user_id = user_id      def __repr__ (self):          return ' User ({}) ' . Format (self.user_id)    users = [user (+), User (3), user (sorted)  Print users  print (users, key = Lambda u:u . user_id) from    operator import attrgetter  print sorted (users, Key=attrgetter (' user_id '))    print min (Users , Key=attrgetter (' user_id '))  print max (users, Key=attrgetter (' user_id '))

1.15 Grouping records according to Fields

rows = [{' Address ': ' 5412 N CLARK ', ' data ': ' 07/01/2012 '},              {' Address ': ' 5232 N CLARK ', ' data ': ' 07/04/2012 '}, {' Address ': ' 5542 E 58ARK ', ' data ': ' 07/02/2012 '},              {' Address ': ' 5152 n Clark ', ' Data ': ' 07/03/2012 '}, {' Address ': ' 7412 n Clark ', ' Data ': ' 07/02/2012 '},              {' Address ': ' 6789 w Clark ', ' Data ': ' 07/03/2012 '}, {' Address ': ' 9008 N Clark ', ' Data ': ' 07/01/2012 '},    {' Address ': ' 2227 W CLARK ', ' data ': ' 07/04/2012 '}] From operator import Itemgetter from itertools import groupby rows.sort (Key=itemgetter ("Data")) for data, items in GR Oupby (Rows, key=itemgetter (' Data ')): print (data) for I in Items:print (", i) from Collec tions Import defaultdict rows_by_date = defaultdict (list) for row in rows:rows_by_date[row[' Data ']].append (row) F or R in rows_by_date[' 07/04/2012 ': print (R) 

1.16 filtering elements in a sequence

MyList = [1,4,-5,10,-7,2,3,-1]  print [n for n in mylist if n > 0] #列表推导式  print [n to n in MyList if n < 0]
  pos = (n for n with mylist if n > 0) #生成器表达式  print pos for  x in POS:      print (x)    values = [' 1 ', ' 2 ', '-3 ', '-', ' 4 ', ' N/A ', ' 5 ']  def is_int (val):      try:          x = Int (val)          return True      except ValueError:          return False  ivals = List (filter (Is_int, values))  print (ivals)    mylist = [1,4,-5,10,-7,2,3,-1]  Import Math  Print [MATH.SQRT (n) for n with MyList if n > 0]    Clip_neg = [n if n > 0 else 0 for N in mylist]
  print Clip_neg    Clip_pos = [n if n < 0 else 0 for N in mylist]  print clip_pos    addresses = [' A ', ' B ', ' C ', ' d ', ' e ', ' f ', ' g ', ' h ']  counts = [0, 3, 4, 1, 7, 6, 1] from  itertools import compress  More5 = [n > 5 for N in counts]  print more5  Print List (compress (addresses, MORE5))

1.17 extracting a subset from a dictionary

Prices = {' ACNE ': 45.23, ' AAPL ': 612.78, ' IBM ': 205.55, ' HPQ ': 37.20, ' FB ': 10.75}  p1 = {Key:value for key, value in price S.items () if value > $  print p1    tech_names = {' AAPL ', ' IBM ', ' HPQ '}  p2 = {Key:value for key, value in Prices.items () if key in Tech_names}  print P2    p3 = dict ((key, value) for key, value in Prices.items () if Value & Gt #慢  Print p3    tech_names = {' AAPL ', ' IBM ', ' HPQ '}  P4 = {Key:prices[key] for key in Prices.keys () if key I N tech_names} #慢  print P4

1.18 mapping names to elements of a sequence

From collections import Namedtuple subscriber = namedtuple (' subscriber ', [' addr ', ' joined ']) Sub = Subscriber (' Wang@qq  . com ', ' 2020-10-10 ') Print sub print sub.joined print sub.addr print len (sub) addr, joined = Sub Print addr Print Joined Def Compute_cost (records): total = 0.0 for rec in Records:total + = rec[1]*rec[2] Retur  N Total Stock = namedtuple (' Stock ', [' name ', ' shares ', ' Price ']) def Compute_cost2 (records): all = 0.0 for  Rec in records:s = Stock (*REC) Total + = S.shares * S.Price return total s = stock (' ACME ', 100, 123.45) print S #s. Shares = #error s = s._replace (shares=75) print s Stock = namedtuple (' Stock ', [' name ', ' s Hares ', ' price ', ' Date ', ' time ']) Stock_prototype = Stock (", 0, 0.0, none, none) def dict_to_stock (s): return stock _prototype._replace (**s) a = {' name ': ' Acme ', ' shares ': +, ' price ': 123.45} print Dict_to_stock (a) b = {' name ': ' Acme ', ' Shares ': +, ' price ': 123. *, ' date ': ' 12/12/2012 '} print Dict_to_stock (b) 

1.19 conversion and conversion of data at the same time

Nums = [1, 2, 3, 4, 5] s = SUM (x*x for x in nums) print s import os files = Os.listdir (' dirname ') if any (name.end  Swith ('. Py ') for name in files): print (' There is python! ')        Else:print (' Sorry, no python! ') s = (' ACME ', ' 123.45 ') print (', '. Join (str (x) for x in s)) portfolio = [{' Name ': ' GOOG ', ' shares ': 50 }, {' name ': ' YHOO ', ' shares ': +}, {' name ': ' AOL ', ' shares ':}, {' Name ': ' Scox ', ' shares ': [+]] min_shares = min (s[' shares ') for s in portfolio) print Min_shares Min_shares = min (Portfolio, Key=lambda s:s[' shares ')) Print min_shares 1.20 merge multiple mappings into a single map Java code a = {' X ': 1, ' Z ': 3} b = {' Y ': 2 , ' Z ': 4} #from Collections import Chainmap from Pip._vendor.distlib.compat import chainmap c = Chainmap (A, b) prin  T (c[' X "]) print (c[' Y"]) print (c[' z ')) #from a value in the first map print len (c) Print List (C.values ()) c[' z '] = c[' W '] = c[' x '] print a #del c['Y '] #error modifying the mapping will always work on the first mapping structure of the list values = Chainmap () values[' x '] = 1 values = Values.new_child () #add a new M AP values[' x '] = 2 values = values.new_child () values[' x '] = 3 #print values print values[' x '] values = Values.paren TS print values[' x '] values = values.parents print values[' x '] a = {' X ': 1, ' Z ': 3} b = {' Y ': 2, ' Z ': 4} merged = Dict (b) Merged.update (a) Print merged[' x '] print merged[' y '] print merged[' z '] a[' x '] = print merged[' x '] #不会反应到合并后    Dictionary a = {' X ': 1, ' Z ': 3} b = {' Y ': 2, ' Z ': 4} merged = Chainmap (A, b) print merged[' x '] a[' x '] = print merged[' x '] #会反应到合并后的字典中
  • 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.