Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

advanced use of tuples in python

  ## Say we have a list of strings we want to sort by the last letter of the string.  strs = ['xc', 'zb', 'yd' ,'wa'] 
  ## Write a little function that takes a string, and returns its last letter. 
  ##This will be the key function (takes in 1 value, returns 1 value).  def MyFn(s):    return s[-1]  
  ##Now pass key=MyFn to sorted() to sort by the last letter:  print sorted(strs, key=MyFn) 
  ## ['wa', 'zb', 'xc', 'yd']
Source by developers.google.com #
 
PREVIOUS NEXT
Tagged: #advanced #tuples #python
ADD COMMENT
Topic
Name
3+8 =