Python - 列表(list) 的 详解 及 代码
来源:互联网 发布:java package private 编辑:程序博客网 时间:2024/06/14 06:26
列表(list) 的 详解 及 代码
本文地址: http://blog.csdn.net/caroline_wendy/article/details/17290001
列表(list)是存储项目的数据结构, 包含很多项, 使用方括号("[]")进行存储;
列表长度,len()函数;
列表末尾增加一项,append()函数;
列表排序,sort()函数;
使用下标索引, 获得或改变一个值;
使用del方法, 删除一个元素;
其他方法参见Python手册;
代码如下:
# -*- coding: utf-8 -*-#====================#File: abop.py#Author: Wendy#Date: 2013-12-03#====================#eclipse pydev, python3.3shoplist = ['apple', 'mango', 'carrot', 'banana']print('I have', len(shoplist), 'items to purchase') #求列表的长度print('These items are:', end=' ') #end表示不是换行, 最后结束的是' 'for item in shoplist: print(item, end=' ')print('\nI also have to buy rice.')shoplist.append('rice')print('My shopping list is now', shoplist) #打印列表[]print('I will sort my list now')shoplist.sort() #首字母排序print('Sorted shopping list is', shoplist)print('The first item I will buy is', shoplist[0])olditem = shoplist[0]del shoplist[0] #删除首元素print('I bought the', olditem)print('My shopping list is now', shoplist) #打印剩下的列表
输出:
I have 4 items to purchaseThese items are:apple mango carrot banana I also have to buy rice.My shopping list is now ['apple', 'mango', 'carrot', 'banana', 'rice']I will sort my list nowSorted shopping list is ['apple', 'banana', 'carrot', 'mango', 'rice']The first item I will buy is appleI bought the appleMy shopping list is now ['banana', 'carrot', 'mango', 'rice']
7 0
- Python - 列表(list) 的 详解 及 代码
- Python 列表list详解
- Python代码分析,对于列表使用list += list 和 list = list + list的不同
- Python 列表(List)操作方法详解
- Python 列表(List)操作方法详解
- Python 列表(List)操作方法详解
- Python 列表(List)操作方法详解
- Python 列表(List)操作方法详解
- Python 列表(List)操作方法详解
- Python 列表(List)操作方法详解
- Python 列表(List)操作方法详解
- Python 列表(List)操作方法详解
- python中的列表(list)详解
- Python 列表(List)操作方法详解
- Python 列表(List)操作方法详解
- Python 列表(List)操作方法详解(转载)
- Python的列表操作List
- Python - 函数(function) 的 代码 及 详解
- fac函数求n的阶乘--C++
- How to clone a scene node
- Websphere的类加载器以及策略的简单介绍
- 用一个sumofsquare函数,辨别是两个整数相加还是两个实数相加---C++重载
- valgrind发现linux下程序的内存管理和线程bugs
- Python - 列表(list) 的 详解 及 代码
- fibonacci 数列--C++
- 简单的linux命令学习
- 【Java并发编程】之十五:并发编程中实现内存可见的两种方法比较:加锁和volatile变量
- 广州传智播客学Java 让人称心如意的好学校
- 编译原理词法分析程序
- Android界面----Tab标签的实现
- Android中RelativeLayout各个属性的含义
- Java学习的重点