彩世界平台-彩世界时时app-彩世界开奖app苹果下载

热门关键词: 彩世界平台,彩世界时时app,彩世界开奖app苹果下载

您的位置:彩世界平台 > 彩世界平台 > Python快速入门(4)排序、字典、文件

Python快速入门(4)排序、字典、文件

发布时间:2019-09-04 22:18编辑:彩世界平台浏览(142)

    08
    Python Sorting:
    1.最简单的排序方式:

    简单的字典排序,根据value值得大小排序:
    
    def sort_dict(dict_words):
        """
        字典排序
        :param dict_words:
        :return:
        """
        keys = dict_words.keys()
        values = dict_words.values()
    
        list_one = [(key, val) for key, val in zip(keys, values)]
        list_sort = sorted(list_one, key=lambda x: x[1], reverse=True)
    
        return list_sort
    
       #sorted(list) function: which takes a list and returns a new list with those elements in sorted order
       a = [5, 1, 4, 3]
       print sorted(a)  ## [1, 3, 4, 5]
       print a  ## [5, 1, 4, 3]
    
       #The sorted() function can be customized though optional arguments. The sorted() optional argument reverse=True, e.g. sorted(list, reverse=True), makes it sort backwards.
       strs = ['aa', 'BB', 'zz', 'CC']
       print sorted(strs)  ## ['BB', 'CC', 'aa', 'zz'] (case sensitive)
       print sorted(strs, reverse=True)   ## ['zz', 'aa', 'CC', 'BB']
    

      

    1. 使用key=func 进行定制排序
      #1.For example with a list of strings, specifying key=len (the built in len() function) sorts the strings by length, from shortest to longest. The sort calls len() for each string to get the list of proxy length values, and the sorts with those proxy values.

      strs = ['ccc', 'aaaa', 'd', 'bb']
      print sorted(strs, key=len)  ## ['d', 'bb', 'ccc', 'aaaa']
      
      #2.As another example, specifying "str.lower" as the key function is a way to force the sorting to treat uppercase and lowercase the same:
      
      ## "key" argument specifying str.lower function to use for sorting
      print sorted(strs, key=str.lower)  ## ['aa', 'BB', 'CC', 'zz']
      
      #3.You can also pass in your own MyFn as the key function, like this:
      
      ## 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']
      
    1. sort()方法

      #the sort() method on a list sorts that list into ascending order
      list.sort()
      #the sort() method changes the underlying list and returns None
      

    元组tuple
    tuple是不可变的,并且大小不能改变
    ##Tuples play a sort of "struct" role in Python -- a convenient way to pass around a little logical, fixed size bundle of values.

        tuple = (1, 2, 'hi')
        print len(tuple)  ## 3
        print tuple[2]    ## hi
        tuple[2] = 'bye'  ## NO, tuples cannot be changed
        tuple = (1, 2, 'bye')  ## this works, it created a new tuple
    

    列表进阶:
    1.

        nums = [1, 2, 3, 4]
        squares = [ n * n for n in nums ]   ## [1, 4, 9, 16]
    
      [ expr for var in nums] -- ## the expr to its left is evaluated once for each element to give the values for the new list.
    

    2.

        strs = ['hello', 'and', 'goodbye']
    
        shouting = [ s.upper() + '!!!' for s in strs ]
        ## ['HELLO!!!', 'AND!!!', 'GOODBYE!!!']
    
    1. 也可以加入if判断语句:

       ## Select values <= 2
       nums = [2, 8, 1, 6]
       small = [ n for n in nums if n <= 2 ]  ## [2, 1]
      
       ## Select fruits containing 'a', change to upper case
       fruits = ['apple', 'cherry', 'bannana', 'lemon']
       afruits = [ s.upper() for s in fruits if 'a' in s ]
       ## ['APPLE', 'BANNANA']
      

    字典dict:{}
    映射类型/哈希表

    ##The contents of a dict can be written as a series of key:value pairs within braces { }, e.g. dict = {key1:value1, key2:value2, ... }. The "empty dict" is just an empty pair of curly braces {}.

    字典是python中唯一的映射类型(哈希表),常用成员方法:
    keys()
    values()
    items()

    本文由彩世界平台发布于彩世界平台,转载请注明出处:Python快速入门(4)排序、字典、文件

    关键词:

上一篇:没有了

下一篇:没有了