列表和数组
来源:互联网 发布:国内旅游收入数据统计 编辑:程序博客网 时间:2024/06/11 08:30
list Python 内置单独的一种数据类型是列表,list是一种有序的集合,可以随时添加和删除其中的元素。classmates = ['Michael', 'Bob', 'Tracy']print classmates;C:\Python27\python.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:\Python27\python.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:\Python27\python.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:\Python27\python.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:\Python27\python.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:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py['Michael', 'Bob', 'Tracy']['Michael', 'Bob', 'Tracy', 'Adam']['Michael', 'Sarah', 'Tracy', 'Adam']Process finished with exit code 0list元素也可以是另一个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:\Python27\python.exe C:/Users/TLCB/PycharmProjects/untitled/a2.py4pythonjava['asp', 'php']schemeaspphpProcess finished with exit code 0tuple另一种有序列表叫元组: tuple.tuple和list非常类似,但是tuple一旦初始化就不能修改,比如同样列出同学的名字:classmates = ('Michael', 'Bob', 'Tracy')print classmates;classmates[0]='xxyy'print classmates;C:\Python27\python.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 assignmentProcess finished with exit code 1
阅读全文
0 0
- 1210 列表和数组
- 数组和列表
- perl列表和数组
- 列表和数组
- Perl数组和列表简介
- PERL中的数组和列表
- TCL中的列表和数组
- R语言--数组和列表
- 数组、集合和散列表
- 数组和列表的选择
- 数组和列表的区别
- 自定义数组列表和队列
- perl学习笔记二----列表和数组
- 第四章 列表和数组变量
- perl 列表和数组变量详解
- perl学习日记3 列表和数组
- perl学习笔记二----列表和数组
- perl学习——列表和数组
- cellspacing和cellpadding
- BiMap
- 【Redis深入】Redis分布式锁的实现
- ELK之logstath的使用(1)----介绍
- ROS 小乌龟测试
- 列表和数组
- Codeforces 665E Beautiful Subarrays (01Trie)
- Java的三种代理模式
- 关于jquery的所有知识
- (转)详解腾讯云CentOS7.0使用yum安装mysql及使用遇到的问题
- SQl 语句(常见) 新建,删除,修改表,新增字段,修改默认值
- list集合转换成json数据,ajax请求返回json数据,再通过循环显示出来
- 实习日志-6
- docker 安装 mysql