• 列表、字典内置方法


    列表内置方法:只能给列表用

    lt = [1, 2, 3,4]
    
    方法 代码
    索引取值/索引修改值 lt[0] = 2 print(lt)
    索引切片 print(lt[0:1])
    成员运算in/not in print(2 in lt) print(2 not in lt)
    for循环 for i in lt:print(i)
    len长度 print(len(lt))
    append():追加值 lt.append(4) print(lt)
    sort():排序 lt.sort() print(lt)
    reverse():反转 lt = [1, 2, 3]lt.reverse()print(lt)
    index():获取元素索引 print(lt.index(2))
    clear():清空列表 lt.clear() print(lt)
    copy():复制 lt = [1, 2, 3] lt1 = lt.copy() print(lt1)
    extend():扩展列表 lt1 = [1, 2, 3] lt2 = [1, 5, 6] lt1.extend(lt2) print(lt1)
    remove(): 移除 lt = [1, 2, 3] lt.remove(3) print(lt)
    insert():按照索引插入值 lt = [1, 2, 3] lt.insert(0, 1.5) print(lt)

    字典内置方法

    banzhuren_info_dic = {'name': 'a', 'height': 21313, 'weight': 1231,                      'hobby_list': ['sdfsdf', 'ads', 'qrt']}
    
    方法 代码
    按key取值 print(banzhuren_info_dic['height'])
    按key修改值 banzhuren_info_dic['height'] = banzhuren_info_dic['height'] + 1 print(banzhuren_info_dic['height'])banzhuren_info_dic['age'] = 30
    按key增加值 anzhuren_info_dic['age'] = 30 print(banzhuren_info_dic)
    成员运算(比较的是key) print('age' in banzhuren_info_dic)
    for循环(对key循环) for i in banzhuren_info_dic: print(i)
    keys() print(banzhuren_info_dic.keys())
    values() print(banzhuren_info_dic.values())
    items() print(banzhuren_info_dic.items())
    get(): 取值 print(banzhuren_info_dic.get('height', 150))
    update() dic1 = {'a':1} dic2 = {'b':2} dic1.update(dic2) print(dic1)
    setdefault(): dic1 = {'a': 1} dic1.setdefault('a', 2) print(dic1)
    s='nbx qwer'
    
    方法 代码
    索引取值 print(s[0])
    切片 print(s[0:4])
    成员运算 print('nbx' in s) print('nbx1' in s)
    for循环 for i in s: print(i)
    len() print(len(s))
    strip() print(s.strip('n* '))
    lstrip()/rstrip() 同上
    find()/index() s = 'nick'print(s.find('a'))
    join() print('*'.join(['a', 'b', 'c']))
    split() s = 'abc' print(s.split('*'))
    center/ljust/rjust print(s.center(50, '-'))
    isdigit()/isalpha() s = 'a'print(s.isdigit()) print(s.isalpha())#判断是否纯
    count() s = 'nick nick' print(s.count('nick'))
  • 相关阅读:
    让Dreamweaver支持cshtml (MVC Razor环境)
    href="#"与href="javascript:void(0)"的区别
    ASP.NET MVC 4 中Razor 视图中JS无法调试 (重要)
    03011_HttpServletRequest
    1004. 成绩排名 (20)
    1003. 我要通过!(20)
    C语言文件读写命令fprintf和fscanf
    1002. 写出这个数 (20)
    1001. 害死人不偿命的(3n+1)猜想 (15)
    汇编in和out实例解析
  • 原文地址:https://www.cnblogs.com/maqiaobin/p/11442900.html
Copyright © 2020-2023  润新知