• python基础31[数据结构list+tuple+set+dictionary]



    默认地,所有index的下标均从0开始。

    一 list
    1) list 基础
    >>> a = ['money''money''money'100000000]
    >>> a
    [
    'money''money''money'100000000]
    >>> a[3]
    100000000
    >>> a[-1= a[-1* 2
    >>> a[-1]
    200000000
    >>> ['i''want'+ a
    [
    'i''want''money''money''money'200000000]
    >>> a
    [
    'money''money''money'200000000]
    >>> a[:0] = ['i''want']
    >>> a
    [
    'i''want''money''money''money'200000000]
    >>> a[2:4= []
    >>> a
    [
    'i''want''money'200000000]
    >>> len(a)
    4
    >>> a[:]= []
    >>> a
    []
    >>>

    从上面的实例中可以可以看到

    list类型的定义a = ['money''money''money'100000000],也可以从set定义list入s = {1,2,3} 和l = list(s)。

    元素的访问,元素的修改,list的链接,元素的插入,元素的删除,求list的长度,list的清空。

    2)list类成员函数

    >>> a = [1020100360]
    >>> a
    [
    1020100360]
    >>> a.append(79)
    >>> a
    [
    102010036079]
    >>> b = [4488]
    >>> a.extend(b)
    >>> a
    [
    1020100360794488]
    >>> a.insert(0, 500)
    >>> a
    [
    5001020100360794488]
    >>> a.remove(79)
    >>> a
    [
    50010201003604488]
    >>> a.index(3)
    4
    >>> a.pop()
    88
    >>> a.count(10)
    1
    >>> a.sort()
    >>> a
    [
    310204460100500]
    >>> a.reverse()
    >>> a
    [
    500100604420103]
    >>>
    注意:
    append用来在结尾增加一个元素。
    extend用来在结尾增加一个list。
    insert在指定的index处插入一个元素。
    remove删除指定值的元素。
    index查找给定值的index。
    pop删除且返回list的最后一个值。
    count获得给定值在list重复的个数。
    sort排序。
    reverse反序。

    3)list作为栈
    >>> mystack = [3,5,1]
    >>> mystack
    [
    351]
    >>> mystack.append(3)
    >>> mystack
    [
    3513]
    >>> mystack.append(2)
    >>> mystack
    [
    35132]
    >>> mystack.pop()
    2
    >>> mystack.pop()
    3
    >>> mystack
    [
    351]
    >>>

    4)list作为队列
    >>> myqueue = ['tom''tom2']
    >>> myqueue
    [
    'tom''tom2']
    >>> myqueue.append('tom3')
    >>> myqueue
    [
    'tom''tom2''tom3']
    >>> myqueue.pop(0)
    'tom'
    >>> myqueue
    [
    'tom2''tom3']
    >>>

    5)list高级

    #list to another list
    numbers = [1,2,3,4,5]
    squares 
    = [number*number for number in numbers]
    print(squares)
    # [1,4,9,16,25]

    numbers_under_4 
    = [number for number in numbers if number < 4]
    print(numbers_under_4)
    # [1,2,3]

    squares_under_4 
    = [number*number for number in numbers if number < 4]
    print(squares_under_4)
    # [1,4,9]

    #list and for, not foreach
    strings = ['a''b''c''d''e']
    for index, string in enumerate(strings):
        
    print (index, string)
    # '0 a 1 b 2 c 3 d 4 e'

    #list any and all
    numbers2 = [1,2,3,4,5,6,7,8,9]
    if all(number < 10 for number in numbers2):
        
    print ('Success!')
    # 'Success!'
    numbers3 = [1,10,100,1000,10000]
    if any(number < 10 for number in numbers3):
        
    print ('Success!')
    #  'Success!'

    #mutiply list
    mat = [[1,2,3],[4,5,6]]
    for i in [0,1,2]:
        
    for row in mat:
            
    print(row[i],end=" ")
        
    print()
    #1 4 
    #
    2 5 
    #
    3 6
    print(list(zip(*mat)))
    #[(1, 4), (2, 5), (3, 6)]

    #list union by set
    numbers4 = [1,2,3,3,4,1]
    print(set(numbers4))
    #{1,2,3,4}
    if len(numbers4) == len(set(numbers4)):
        
    print ('List is unique!')

    注意:

    list 到list的映射。

    any和all在list中的使用。

    list与for的结合,相当于高级语言中的foreach功能。

    对list的enumerate与for的结合,相当于高级语言中的for功能。

    list的嵌套使用,相当于多维数组。
    del相当于list本身的remove,都可以用来删除list中的元素。

    二 tuple
    >>> t = 123350
    >>> t[2]
    50
    >>> t
    (
    123350)
    >>> u = (t, 60)
    >>> u
    ((
    123350), 60)
    >>> len(u)
    2
    >>> x,y,z = t
    >>> x
    12
    >>> y
    33
    >>> z
    50
    >>> [3*for i in t]
    [
    3699150]
    >>> t.count(50)
    1
    >>> t.index(50)
    2

    注意:

    tuple相当于是不可修改的list,所以同时也不具有list的很多修改的方法,例如append,insert,remove等;但是tuples仍然可以使用count,index等查找方法。
    使用 t2 = (2,4,8)来定义tuple,也可以使用list来定义tuple如 l = [1,2,3] 和 t = tuple(l)。


    三 set

    >>> basket = {'apple''orange''apple''pear''orange''banana'}
    >>> print(basket)
    {
    'orange''pear''banana''apple'}
    >>> a = [1,2,30,45,69]
    >>> b = set(a)
    >>> b
    {
    12453069}
    >>> 'pear' in basket
    True
    >>> c = {x for x in 'abracadabra' if x not in 'abc'}
    >>> c
    {
    'r''d'}
    >>> a = {3020,1}
    >>> a
    {
    12030}
    >>> a - b
    {
    20}
    >>> a | b
    {
    1269452030}
    >>> a & b
    {
    130}
    >>> a ^ b
    {
    2694520}
    >>>
    注意:
    set的定义使用{}。
    可以将list使用set显示的转化为set。
    可以用in来判断指定元素是否存在于set中。
    set于for结合使用,相当于foreach。
    set可以进行|,&,^,-操作。

    三 dictionary
    >>> d = {1 : 'tom'2 : 'bill'}
    >>> d
    {
    1'tom'2'bill'}
    >>> d[3= 'god'
    >>> d
    {
    1'tom'2'bill'3'god'}
    >>> d[2]
    'bill'
    >>> del d[2]
    >>> d
    {
    1'tom'3'god'}
    >>> list(d.keys())
    [
    13]
    >>> 1 in d
    True
    >>> dict([('sape'4139), ('guido'4127), ('jack'4098)])
    {
    'sape'4139'jack'4098'guido'4127}
    >>> {x: x**2 for x in (246)}
    {
    24416636}
    >>> dict(sape=4139, guido=4127, jack=4098)
    {
    'sape'4139'jack'4098'guido'4127}
    >>>
    注意:
    dictionary使用{1 : 'tom'2 : 'bill'}来定义,也可以使用dict([('sape'4139), ('guido'4127), ('jack'4098)])来从list定义dictionary
    []=来增加新的值。
    del来删除指定的key和value。
    in用来查找key是否存在。

    keys()用来查看所有的keys。

    #dictionary
    keyGenDict={1:'blue',4:'fast',2:'test'}
    print (keyGenDict.keys())
    print (keyGenDict.values())
    print (keyGenDict.items())

    keys
    =list(keyGenDict.keys())
    keys.sort()
    for key in keys:
        
    print(keyGenDict[key])
        
    for key, value in keyGenDict.items():
        
    print(key,value)

    dictionary的keys,values和items的使用。


    四 other
    >>> a = [1,7,9,2]
    >>> for x in a:
        
    print(x, end = " ")

    1 7 9 2 
    >>> s = {3,6,9,1}
    >>> for i in s:
        
    print(i, end = " ")

    1 3 6 9 
    >>> d = {'aa':1'bb':2'cc':3}
    >>> for x in d:
        
    print(x,end = ' ')

    aa cc bb 
    >>> for k,v in d.items():
        
    print(k,v)

    aa 
    1
    cc 
    3
    bb 
    2
    >>> for x in enumerate(a):
        
    print(x,end = ' ')

    (0, 
    1) (17) (29) (32
    >>> questions = ['name''quest''favorite color']

    >>> answers = ['lancelot''the holy grail''blue']

    >>> for q, a in zip(questions, answers):
         
    print('What is your {0}?  It is {1}.'.format(q, a))

    What 
    is your name?  It is lancelot.
    What 
    is your quest?  It is the holy grail.
    What 
    is your favorite color?  It is blue.
    >>> for f in sorted(s):
        
    print(f,end=' ')

    1 3 6 9 
    >>>

    dictionary的items()可以在遍历时同时获得key和value。
    enumerate可以在遍历时增加index。
    zip可以将多个list绑定到一个遍历中。
    sorted可以在遍历时排序。

    完!

    作者:iTech
    微信公众号: cicdops
    出处:http://itech.cnblogs.com/
    github:https://github.com/cicdops/cicdops

  • 相关阅读:
    jquery ajax 返回数据时 ff正常,ie接受到数据但是显示不了
    查看IIS日志并且分析其中的错误日志
    用eventvwr查看系统日志
    C++实现Trie 树
    [算法之美笔记02] 栈模拟网页的前进后退 ; 阻塞队列与并发队列
    MySQL学习小记(三) 结合JDBC实现用户的登录响应
    [算法之美笔记01] 数组,链表的删除和垃圾回收,缓存机制有什么关系
    [埋坑系列] 基于QT/C++的杰瑞走迷宫小游戏 :1.大体构造
    品味C++实现AVL树的删除操作
    C++实现AVL树的四种旋转
  • 原文地址:https://www.cnblogs.com/itech/p/1559525.html
Copyright © 2020-2023  润新知