zoukankan      html  css  js  c++  java
  • Python--列表操作

    # #-*- coding: utf-8 -*-
    '''
    @author: soyo
    '''
    import numpy
    import pandas 
    import math as m
    from _ctypes import Array
    from _collections import deque
    a="soyo"
    b=200
    s=0
    c=b%6
    d=m.sqrt(25)
    while b:
        s+=b 
        b-=1
    print s
    print d
    for a in ['e','f','g']:
        print a
    for a in "soyo":
        print a
    for a in [1,2,"soyo",8,9]:
        print a
        if a=="soyo":
            for b in a:
                print b 
    f=range(20)
    for a in f:
        print a      
        if a==10:
            print "soyo88"
    w=range(3,8)
    for a in w:
        print a 
    r=range(2,15,3)
    for a in r:
        print a
    print "*************"  
    l=[1,2,3]
    for a in l:
        a+=1;
        print a  
    print l
    print "******************"
    for i in range(len(l)):
        l[i]+=1
        print l[i]    
    else:
        print "soyosoyo"
    print l
    print len(l)    
    print "************列表操作***********"                        
    print "列表切片操作:"
    p=[1,2,4,5,22,66,78,89,"soyo",56,22,88,22]
    p3=["soyo2",778,999]
    print p[0]
    print p[1]
    print p[-1]
    print p[-2]
    p2=p[1:5]
    print p2
    print "列表添加操作"
    p.append(100)
    print p
    print "/*/*/*/*/*"
    p.append(p2)      #想要去掉新列表中的[]改用p.extend(p2)
    print p 
    print "/*/*/*/*/*"
    print p[-1]
    for a in p[-1]:
        print a
    p.insert(2, "soyo5")
    print p
    p.remove(4)
    print p
    p.pop(2)
    print p
    print p.index("soyo")
    print "//////////"
    print p
    print p.count(2)
    p.sort()
    print p
    p.reverse()
    print p
    print "列表 用作栈"
    p4=[1,2,5]
    p4.append(20)
    p4.append(98)
    print p4
    p4.pop()
    print p4.pop()  #出栈的元素
    print p4
    print "列表 用作队列" #可以实现但是效率不高
    print "deque 用作队列"  #队列实现改用deque
    p6=deque([8,9,10])
    p6.append(11)
    p6.append(12)
    print p6
    p6.popleft()
    print p6.popleft()
    print p6

    结果:20100
    5.0
    e
    f
    g
    s
    o
    y
    o
    1
    2
    soyo
    s
    o
    y
    o
    8
    9
    0
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    soyo88
    11
    12
    13
    14
    15
    16
    17
    18
    19
    3
    4
    5
    6
    7
    2
    5
    8
    11
    14
    *************
    2
    3
    4
    [1, 2, 3]
    ******************
    2
    3
    4
    soyosoyo
    [2, 3, 4]
    3
    ************列表操作***********
    列表切片操作:
    1
    2
    22
    88
    [2, 4, 5, 22]
    列表添加操作
    [1, 2, 4, 5, 22, 66, 78, 89, 'soyo', 56, 22, 88, 22, 100]
    /*/*/*/*/*
    [1, 2, 4, 5, 22, 66, 78, 89, 'soyo', 56, 22, 88, 22, 100, [2, 4, 5, 22]]
    /*/*/*/*/*
    [2, 4, 5, 22]
    2
    4
    5
    22
    [1, 2, 'soyo5', 4, 5, 22, 66, 78, 89, 'soyo', 56, 22, 88, 22, 100, [2, 4, 5, 22]]
    [1, 2, 'soyo5', 5, 22, 66, 78, 89, 'soyo', 56, 22, 88, 22, 100, [2, 4, 5, 22]]
    [1, 2, 5, 22, 66, 78, 89, 'soyo', 56, 22, 88, 22, 100, [2, 4, 5, 22]]
    7
    //////////
    [1, 2, 5, 22, 66, 78, 89, 'soyo', 56, 22, 88, 22, 100, [2, 4, 5, 22]]
    1
    [1, 2, 5, 22, 22, 22, 56, 66, 78, 88, 89, 100, [2, 4, 5, 22], 'soyo']
    ['soyo', [2, 4, 5, 22], 100, 89, 88, 78, 66, 56, 22, 22, 22, 5, 2, 1]
    列表 用作栈
    [1, 2, 5, 20, 98]
    20
    [1, 2, 5]
    列表 用作队列
    deque 用作队列
    deque([8, 9, 10, 11, 12])
    9
    deque([10, 11, 12])


  • 相关阅读:
    简单的PHP+SMARTY分页类
    三大WEB服务器对比分析(apache ,lighttpd,nginx)
    逐步设置vim C/C++语法高亮显示和自动缩进
    Lighttpd 的安装配置(web服务器软件)
    ASP如何获取文件和目录空间大小
    innerHtml用法
    临时表操作的一些见解(解决了我在存储过程中使用临时表的困惑)
    Javascript的IE和Firefox兼容性汇编
    Javascript的IE和Firefox兼容性汇编
    innerHtml用法
  • 原文地址:https://www.cnblogs.com/soyo/p/8126141.html
Copyright © 2011-2022 走看看