Learn Python By Practice — 排序和元组,pythonpractice,排序和元组'''创建于2


排序和元组

'''创建于2012年11月28日1@author: jiangxiaoqiang本篇主要阐述排序、元组、list和tuple的unpack和list comprehensions'''#coding = utf-8import sysdef sorttest():    # 最简单的情况    a = [4, 2, 3, 5, 7, 9, 8, 1, 6]    print('排序后:' + str(sorted(a)))    print('a不变:' + str(a))    print('-----------------------------------')    strs = ['aabfefefe', 'bbed', 'eewsd', 'ddwewx', 'fff']    print('排序后:' + str(sorted(strs)))    print('strs不变:' + str(strs))    print('-----------------------------------')    # 指定排序选项reverse    print('reverse排序strs:')    print(sorted(strs, reverse=True))    print('-----------------------------------')    # 指定排序选项key    print('key排序strs:')    print(sorted(strs, key=len)) # len为全局函数    print('-----------------------------------')    print('key排序strs2,忽略大小写:')    strs2 = ['aa', 'cc', 'DD', 'BB']    print(sorted(strs2, key=str.lower))    print('-----------------------------------')    # 自定义排序规则    print('自定义排序规则排序:')    def MyFn(s):        return s[1]    b = ['wa', 'ha', 'ok', 'ko', 'ah']    print(sorted(b, key=MyFn))    print('-----------------------------------')    # sort()有点特殊,它会改变列表,并且返回None    print('sort()排序:')    alist = [1, 3, 4, 5, 8, 7, 6]    blist = alist.sort()    print('alist: ' + str(alist))    print('blist: ' + str(blist))    print('-----------------------------------')    # Tuple(元组)    #  Tuples are like lists, except they are immutable and do not change size     # (tuples are not strictly immutable since one of the contained elements could be mutable).     # Tuples play a sort of 'struct' role in Python     # -- a convenient way to pass around a little logical, fixed size bundle of values.     # A function that needs to return multiple values can just return a tuple of the values.     print('元组:')    myTuple = (1, 2, 3)    print(len(myTuple))    print(myTuple[1])    # myTuple[2] = 'hello' # TypeError: 'tuple' object does not support item assignment    myTuple = (1, 2, 'hello')    print(myTuple[2])    print('-----------------------------------')    # 创建只有一个元素的元组,注意后面的英文逗号不能省略    print('一个元素的元组:')    myTuple2 = ('hi')    print(myTuple2[0])   # h    print(len(myTuple2)) # 2    myTuple2 = ('hi',)    print(myTuple2[0])   # hi    print(len(myTuple2)) # 1    print('-----------------------------------')    # 列表(list)的unpack    print('list的unpack:')    [x, y, z] = ['a', 'b', 'c']    # [x, y, z] = ['a', 'b'] # ValueError: need more than 2 values to unpack    print(x)    print(y)    print(z)    print('-----------------------------------')    # 元组(tuple)的unpack    print('tuple的unpack:')    (x, y, z) = ('jxq', 'love', 'java')    print(x)    print(y)    print(z)    print('-----------------------------------')    # list comprehensions    nums = [1, 2, 3, 4]    sqrs = [n*n for n in nums]    print(sqrs[3])    print('-----------------------------------')    strs = ['JXQ', 'William', 'feichexia']    greetings = ['Hello, '+s.lower()+'!' for s in strs]    print(greetings[1])    # 可以添加过滤条件,这样就同时具有filter和map功能了    greetings = ['Hello, '+s.lower()+'!' for s in strs if len(s)>3]    print(greetings[0])    print('-----------------------------------')def main():    sorttest()if __name__=='__main__':    main()

评论关闭