zoukankan      html  css  js  c++  java
  • Python学习笔记_list&dictionary

    list

    list.reverse()

    list.sort()

    list.index(obj):obj is the object to be find out; it returns index of the found object otherwise raise an exception indicating that value does not find.

    aList = [123, 'xyz', 'zara', 'abc'];
    
    print "Index for xyz : ", aList.index( 'xyz' ) ; //return  1
    print "Index for zara : ", aList.index( 'zara' ) ;//return  2

    list.append(x)

    list.insert(i, x):insert x after list[i].

    list.pop():the last item will be removed.

    list.pop(3) :delete the list[3].the argument is index.so sometimes, you may use list.pop(list.index(obj))

    list.remove(sth):delete the 'sth'.

    list.extend(an_iter):add each item from the iterable an_iter onto the list.

    Difference between extend and append:

    iteration

    warning: we can't modify a list while interating over it.

    settle: create another list

    dictionary

    A dictionary is an unordered collection of key: value pairs, with each key in a dictionary being distinct.

    key:immutable

    value: mutable

    Convert Iterable of Pairs into Dictonary 

    dict() 、dict(an_iter)

    将一对值转换为dict的key-value形式,前提是key是immutable的。如果一个key值出现了多次,那么这个key对应的value为最后一个key出现时对应的值。如果要转换的值已经是一个dict了,那么就返回一个这个dict的副本。

    Get Value in Dictionary by Key

    dict[key]、dict.get(key)、dict.get(key, default)

    如果key不在这个dictionary里,就返回一个keyError信息;如果提供了default值,那就返回default值。

    Remove Key from Dictionary and Return its Value

    dict.pop(key)、dict.pop(key, default)

    删掉key对应的value,并返回value

    Get List of all Key/Value Pairs in Dictionary

    dict.items()

    返回这个dict中得所有键值对,(key, value)的list形式,经常用于for循环中。

  • 相关阅读:
    【小白建站笔记】从购买域名及服务器到备案成功:以阿里云为例
    jarvisoj-Evil Exe
    jarvisoj-软件密码破解-2(CFF_100_1)
    Jarvis OJ-superexpress
    JarvisOJ-[xman2019]xfz
    csictf-Esrever
    分解质因数
    矩阵乘法
    python获取一组数个位数之和
    python计算字母出现次数
  • 原文地址:https://www.cnblogs.com/beatrice7/p/4045336.html
Copyright © 2011-2022 走看看