序列是python中的基础数据结构,序列里每一个元素都有一个下标,从0开始,依次递增.

list,tuple,dictionary是使用最频繁的三类数据结构。

(1)序列都有的方法包括:索引,切片,检查成员,加,乘:

 #!/usr/bin/env python
# -*- coding: UTF-8 -*-
#索引
list_name = ['Paul', 'John', 'James']
print(list_name[1]) #切片
list_number = [1, 1, 2, 3, 4, 5]
print(list_number[1:5]) #检查成员
if 'Joshua' in list_name:
print("Joshua is in list_name")
else:
print("Joshua is not in list_name") #加
print(list_name + list_number) #乘
print(list_name * 2)

Code

 John
[1, 2, 3, 4]
Joshua is not in list_name
['Paul', 'John', 'James', 1, 1, 2, 3, 4, 5]
['Paul', 'John', 'James', 'Paul', 'John', 'James']

Result

(2)遍历列表:

 #遍历列表
for number in list_number:
print(number)

Code

(3)list的函数有len(),max(),min(),list()

 #函数
print(len(list_number))
print(max(list_number))
print(min(list_number))
tuple_number = (1, 3, 5, 7)
print(type(tuple_number), type(list(tuple_number)))#list()强制将序列转化为list类型

Code

 6
5
1
<class 'tuple'> <class 'list'>

Result

(4)list的常用方法有

-append(), extend(),insert();

 #append(), extend(),insert();
list_number = [1, 1, 2, 3, 4, 5]
list_number.append(7) #在列表末尾添加新的对象
print(list_number)
list_number.extend([10, 11, 12]) #在列表末尾一次性追加另一个序列中的多个值(用新列表扩展原来的列表)
print(list_number)
list_number.insert(1, 99) #list.insert(index, obj)将对象插入列表中index位置
print(list_number)

Code

 [1, 1, 2, 3, 4, 5, 7]
[1, 1, 2, 3, 4, 5, 7, 10, 11, 12]
[1, 99, 1, 2, 3, 4, 5, 7, 10, 11, 12]

Result

-remove(),pop(),clear();

 #remove(),pop(),clear();
list_number = [1, 1, 2, 3, 4, 5]
list_number.remove(1)#移除列表中某个值的第一个匹配项
print(list_number)
pop_number = list_number.pop(4)#list.pop(obj=list[-1]) 移除列表中的索引位置元素(默认最后一个元素),并且返回该元素的值
print("pop number:", pop_number)
print(list_number)
list_number.clear()#清空列表
print(list_number)

Code

 [1, 2, 3, 4, 5]
pop number: 5
[1, 2, 3, 4]
[]

Result

-sort(),reverse();

 #sort(),reverse();
list_number = [4, 2, 5, 7, 1, 3]
print(list_number)
list_number.sort() #对原列表进行排序
print(list_number)
list_number.reverse() #反向原列表中元素
print(list_number)

Code

 [4, 2, 5, 7, 1, 3]
[1, 2, 3, 4, 5, 7]
[7, 5, 4, 3, 2, 1]

Result

-count(),index(),copy()

 #count(),index(),copy()
list_number = [1, 1, 2, 2, 2, 3, 3, 3, 3]
print(list_number.count(3)) #统计某个元素在列表中出现的次数
print(list_number.index(2)) #从列表中找出某个值第一个匹配项的索引位置
print("address of list_number:", id(list_number))
copy_list_number = list_number.copy() #复制列表
print(copy_list_number)
print("address of copy_list_number:", id(copy_list_number))

Code

 4
2
address of list_number: 35406832
[1, 1, 2, 2, 2, 3, 3, 3, 3]
address of copy_list_number: 35421720

Result

最后来看下list类的定义:

 class list(object):
"""
list() -> new empty list
list(iterable) -> new list initialized from iterable's items
"""
def append(self, p_object): # real signature unknown; restored from __doc__
""" L.append(object) -- append object to end """
pass def count(self, value): # real signature unknown; restored from __doc__
""" L.count(value) -> integer -- return number of occurrences of value """
return 0 def extend(self, iterable): # real signature unknown; restored from __doc__
""" L.extend(iterable) -- extend list by appending elements from the iterable """
pass def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
"""
L.index(value, [start, [stop]]) -> integer -- return first index of value.
Raises ValueError if the value is not present.
"""
return 0 def insert(self, index, p_object): # real signature unknown; restored from __doc__
""" L.insert(index, object) -- insert object before index """
pass def pop(self, index=None): # real signature unknown; restored from __doc__
"""
L.pop([index]) -> item -- remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
"""
pass def remove(self, value): # real signature unknown; restored from __doc__
"""
L.remove(value) -- remove first occurrence of value.
Raises ValueError if the value is not present.
"""
pass def reverse(self): # real signature unknown; restored from __doc__
""" L.reverse() -- reverse *IN PLACE* """
pass def sort(self, cmp=None, key=None, reverse=False): # real signature unknown; restored from __doc__
"""
L.sort(cmp=None, key=None, reverse=False) -- stable sort *IN PLACE*;
cmp(x, y) -> -1, 0, 1
"""
pass

Class List

最新文章

  1. Linux环境下部署完JDK后运行一个简单的Java程序
  2. java基本输入输出练习
  3. Docker之Linux Namespace
  4. Unity3D 降低IL2CPP编译可执行文件大小
  5. The Bottom of a Graph(tarjan + 缩点)
  6. mvc-3模型和数据(2)
  7. 中颖4位MCU的减法汇编指令
  8. HDU 4046 Panda
  9. linux下查看所有用户以及用户组
  10. sphinx-2.2.10-1.rhel6.x86_64 rpm包安装的位置
  11. android网络请求库volley方法详解
  12. 为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?
  13. Java动态解压zip压缩包
  14. 机器学习-KNN分类器
  15. 使用json文件给es中导入数据
  16. 引入CSS的三种方式
  17. CAS 单点登录【1】入门
  18. 【做题】codechefCOUNTARI——分块FFT
  19. SpringMVC深度探险(一) —— SpringMVC前传
  20. python后端工程师 数据爬虫

热门文章

  1. keepalived+haproxy 安装配置
  2. 跨域详解之-----Jsonp跨域
  3. Spark运行模式_本地伪集群运行模式(单机模拟集群)
  4. Django学习之mysql增删改查
  5. lr中常用函数以str开头函数
  6. HyperLedger Fabric 1.4 问题汇总(16)
  7. 北京Uber优步司机奖励政策(1月12日)
  8. libevent学习二(Working with an event loop)
  9. datawindow自动换行打印,需结合该函数一起使用
  10. java 创建具有参数化类型的数组