zoukankan      html  css  js  c++  java
  • 列表和数组

    list 
    Python 内置单独的一种数据类型是列表,list是一种有序的集合,可以随时添加和删除其中的元素。
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    print len(classmates)
    
    打印数组长度
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    print len(classmates)
    
    print classmates[0];
    print classmates[1];
    print classmates[2];
    print  classmates[3];
    
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled/a2.py", line 8, in <module>
        print  classmates[3];
    IndexError: list index out of range
    
    数组越界:
    
    list是一个可变的有序表,所以,可以往list中追加元素到末尾:
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    classmates.append('Adam');
    print classmates;
    
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    ['Michael', 'Bob', 'Tracy', 'Adam']
    
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    classmates.append('Adam');
    print classmates;
    
    classmates.insert(1, 'Jack')
    print classmates;
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    ['Michael', 'Bob', 'Tracy', 'Adam']
    ['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
    
    
    要删除list末尾的元素,用pop()方法:
    
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    classmates.append('Adam');
    print classmates;
    
    classmates.insert(1, 'Jack')
    print classmates;
    
    print  classmates.pop();
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    ['Michael', 'Bob', 'Tracy', 'Adam']
    ['Michael', 'Jack', 'Bob', 'Tracy', 'Adam']
    Adam
    
    要删除指定位置的元素,用pop(i)方法,其中i是索引位置:
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    classmates.append('Adam');
    print classmates;
    classmates.pop(1)
    print classmates;
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    ['Michael', 'Bob', 'Tracy', 'Adam']
    ['Michael', 'Tracy', 'Adam']
    
    Process finished with exit code 0
    
    
    要把某个元素替换成别的元素,可以直接赋值给对应的索引位置:
    
    classmates = ['Michael', 'Bob', 'Tracy']
    print classmates;
    classmates.append('Adam');
    print classmates;
    classmates[1] = 'Sarah'
    print classmates;
    
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ['Michael', 'Bob', 'Tracy']
    ['Michael', 'Bob', 'Tracy', 'Adam']
    ['Michael', 'Sarah', 'Tracy', 'Adam']
    
    Process finished with exit code 0
    
    
    list元素也可以是另一个list,比如:(2维数组):
    
    s = ['python', 'java', ['asp', 'php'], 'scheme'];
    print len(s);
    print s[0];
    print s[1];
    print s[2];
    print s[3];
    print s[2][0];
    print s[2][1];
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    4
    python
    java
    ['asp', 'php']
    scheme
    asp
    php
    
    Process finished with exit code 0
    
    tuple
    
    另一种有序列表叫元组: tuple.tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样列出同学的名字:
    
    classmates = ('Michael', 'Bob', 'Tracy')
    print classmates;
    
    classmates[0]='xxyy'
    print classmates;
    
    C:Python27python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py
    ('Michael', 'Bob', 'Tracy')
    Traceback (most recent call last):
      File "C:/Users/TLCB/PycharmProjects/untitled/a2.py", line 4, in <module>
        classmates[0]='xxyy'
    TypeError: 'tuple' object does not support item assignment
    
    Process finished with exit code 1
    

  • 相关阅读:
    百度相关应用
    超实用js代码段一
    js模块化开发
    常见注入手法第一讲EIP寄存器注入
    异常处理第一讲(SEH),筛选器异常,以及__asm的扩展,寄存器注入简介
    32位汇编第六讲,OllyDbg逆向植物大战僵尸,快速定位阳光基址
    32位汇编第五讲,逆向实战干货,(OD)快速定位扫雷内存.
    32位汇编第四讲,干货分享,汇编注入的实现,以及快速定位调用API的数量(OD查看)
    32位汇编第三讲,RadAsm,IDE的配置和使用,以及汇编代码注入方式
    32位汇编第二讲,编写窗口程序,加载资源,响应消息,以及调用C库函数
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13349627.html
Copyright © 2011-2022 走看看