• python基础知识


    list和dict的基础操作:

    '''
    对于list的排序,python提供了两种方式
    list.sort(key,reverse)
    sorted(list,key,reverse)
    sorted 返回一个对象,可以用作表达式,改变了原来的list生成一个新的list
    list.sort()不会返回对象,改变的是原来的list
    reverse 默认值是False默认正序排列,如果设为True,为倒序排列
    '''
    L1=[4,3,1,2]
    L1.sort()
    print(L1)
    
    L2=['abc','423','we2','123']
    L2.sort()
    print(L2)
    
    #根据第二个关键字排序
    L3=[('a',3),('b',1),('c',2)]
    L3.sort(key=lambda x:x[1])
    print(L3)
    #根据第一个关键字排序
    L3.sort(key=lambda x:x[0])
    print(L3)
    
    
    #根据字典的value 排序
    L4=[{'name':100},{'name':97},{'name':98}]
    L4.sort(key=lambda x:x['name'])
    print(L4)
    
    #先根据第二个关键字排序,再根据第一个关键字排序
    L5=[('c',1),('b',2),('a',2),('d',0)]
    L5.sort(key=lambda x:(x[1],x[0]))
    print(L5)
    
    L6=['123','45','678','23']
    L6.sort(key=lambda x:x[1])
    print(L6)
    
    '''list切片'''
    L=[0,1,2,3,4,5,6,7,8,9]
    
    print(L[1:3])
    print(L[:3])
    print(L[3:])
    print(L[3::2])
    print(L[::-1])
    
    
    '''字符串排序'''
    str='string'
    l=list(str)
    print(l)
    l.sort(reverse=True)
    str1=''.join(l)
    print(str1)
    
    
    '''dict相关知识,dict 本身是无序的,可以根据keys、values、items排序'''
    list1=['nana','tang','long']
    list2=[98,100,96]
    dict1=dict(zip(list1, list2))
    print(dict1)
    print(list(dict1.keys()))
    print(list(dict1.values()))
    items=list(dict1.items())
    print(items)
    items.sort(key=lambda x:x[0])
    print(items)
    items.sort(key=lambda x:x[1])
    print(items)
    dict2=dict(items)
    print(dict2)

    结果:

    [1, 2, 3, 4]
    ['123', '423', 'abc', 'we2']
    [('b', 1), ('c', 2), ('a', 3)]
    [('a', 3), ('b', 1), ('c', 2)]
    [{'name': 97}, {'name': 98}, {'name': 100}]
    [('d', 0), ('c', 1), ('a', 2), ('b', 2)]
    ['123', '23', '45', '678']
    [1, 2]
    [0, 1, 2]
    [3, 4, 5, 6, 7, 8, 9]
    [3, 5, 7, 9]
    [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
    ['s', 't', 'r', 'i', 'n', 'g']
    tsrnig
    {'nana': 98, 'tang': 100, 'long': 96}
    ['nana', 'tang', 'long']
    [98, 100, 96]
    [('nana', 98), ('tang', 100), ('long', 96)]
    [('long', 96), ('nana', 98), ('tang', 100)]
    [('long', 96), ('nana', 98), ('tang', 100)]
    {'long': 96, 'nana': 98, 'tang': 100}
    
  • 相关阅读:
    Centos Another app is currently holding the yum lock
    Centos 重置密码
    Effective c#学习笔记(1)
    浅谈计算机编码
    mongodb java spring data
    VS2013 好用的插件
    xml存储bug
    VS 2008 生成操作中各个选项的差别
    程序权限控制
    给钛度产品的一些建议(Note)
  • 原文地址:https://www.cnblogs.com/tangqiu/p/7687075.html
Copyright © 2020-2023  润新知