zoukankan      html  css  js  c++  java
  • python的一致性(1)sorted和len

      每个语言,都有自己的特性,而一致性,是python语言特性重要的一个环节。

      比如排序,我们不是aaa.sort(),而是 sorted(aaa),比如len,不是aaa.length(),而是len(aaa)

    今天介绍一下sorted的一个特性:

    def takeSecond(elem):
    
        if (len(elem)>3):
            return elem[3]
        else:
            return elem[0]
    
    # random list
    random = ['wcf1','wcf2','wcf3','wcf3','wcf2','wcf9','wc','10w']
    
    # sort list with key
    sortedList = sorted(random, key=takeSecond)
    
    # print list
    print('Sorted list:', sortedList)

    这是通过指定key,来指定排序依据。

    再比如:

    import random
    
    class myfriend:
        def __len__(self):
            i = random.randint(3, 6)
            return i
    
    mycls=myfriend()
    print(len(mycls))
    print(len(mycls))
    print(len(mycls))
    print(len(mycls))
    print(len(mycls))
    print(len(mycls))
  • 相关阅读:
    css常用属性记录
    js字符串常用方法总结
    mongoose基本操作
    本地存储API
    历史相关API
    自定义播放器
    HTML5全屏操作API
    HTML5自定义属性操作
    HTML5类操作
    案例:3D切割轮播图
  • 原文地址:https://www.cnblogs.com/aomi/p/7168674.html
Copyright © 2011-2022 走看看