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循环中。

  • 相关阅读:
    23.Vue技术栈开发实战-Icon组件
    shell脚本每行后面多了一个^M的原因和解决办法
    mmap概述
    camera otp介绍
    brk实现
    USB技术浅析
    带你遨游USB世界
    echarts 更换主题颜色
    Hive UDAF介绍与开发
    2020湖北高考理科第一名唐楚玥的学习方法演讲
  • 原文地址:https://www.cnblogs.com/beatrice7/p/4045336.html
Copyright © 2011-2022 走看看