参考文档 https://docs.python.org/zh-cn/3.7/tutorial/introduction.html#lists

"""

DATA STRUCTURE

Container: Sequence

—— String、List & Tuple

List can be Changed or Updated. But, Tuple can not be.

To define a list, you can use symbol '[ ]', tuple use symbol '( )'

数据结构

容器:序列

—— 字符串、列表 & 元组

列表是可变序列。而元组不可以。

定义一个列表,可以使用符号'[ ]',元组则使用符号'( )'

"""


列表的用法

  • 作为 list 用

    见演示代码

  • 作为队列用

    队列 (queue) 作为一种线性的数据结构,其特点是 FIFO (先进先出),队列只能在队头做删除操作,在队尾做插入操作。

    在 Python 中,如果需要用到队列,则需要使用 collections.deque() 。deque 为双端队列,即在队头和队尾均可进行插入或删除操作。

  • 作为栈用

    栈(stack) 作为一种线性的数据结构,其特点是 FILO (先进后出),其限定只能在栈顶进行插入和删除操作。

    栈顶与栈底:允许元素插入与删除的一端称为栈顶,另一端称为栈底。

    压栈:栈的插入操作,叫做进栈,也称压栈、入栈。

    弹栈:栈的删除操作,也叫做出栈。

    在 Python 中,使用 list.append() 即为压栈;使用 list.pop() 即为出栈,注意该方法是返回出栈的内容。


演示代码

#! /usr/bin/python
# coding:utf-8 class ListAction: def __init__(self):
pass @staticmethod
def list_init():
greeting = [None]
print(greeting) @staticmethod
def list_index(): # note: Do not be out of range
# 注意:别越界
greeting = 'hello'
print(greeting[0]) @staticmethod
def list_slicing():
greeting = 'hello'
print(greeting[2:3]) @staticmethod
def list_slicing_step():
# note: the third num means the step.The num is 1 Default.
# 注意:第三位数意味着步长,默认步长为1
greeting = 'hello'
print(greeting[::2]) @staticmethod
def list_connect():
greeting = 'hello'
friend = 'world'
print(greeting + '' + friend) @staticmethod
def list_repeat():
greeting = 'hello'
print(greeting * 3) @staticmethod
def list_in():
greeting = input("Input one character:")
if 'o' in str(greeting):
print(True)
elif 'O' not in str(greeting):
print('O is not in the list')
else:
print('Input character O') @staticmethod
def list_length():
greeting = input("Input something,I can calculate the length:")
# 字符串拼接。注意类型须相同
print('OK, Length is' + ' ' + str(len(greeting))) @staticmethod
def to_list(para):
print(list(para)) @staticmethod
def list_to_string(lst):
try:
to_string = ''.join(lst)
print(to_string)
except TypeError:
print('must be string-list') @staticmethod
def act():
lst = list()
lst.append('li')
lst.extend(['st'])
print(lst) if __name__ == '__main__':
ListAction.list_init()
ListAction.list_index()
ListAction.list_slicing()
ListAction.list_repeat()
ListAction.list_slicing_step()
ListAction.list_in()
ListAction.list_connect()
ListAction.list_length()
ListAction.to_list('HELLO')
ListAction.list_to_string(lst=['1', '2'])
ListAction.act()

对列表进行分段

In [1]: def te():
...: a = [1,2,3,4,5,6,7,8,9]
...: matrix = []
...: for i in range(0,len(a),5):
...: matrix.append(a[i:i+5])
...: print(matrix)
...: In [2]: te()
[[1, 2, 3, 4, 5], [6, 7, 8, 9]]

对两个列表的处理

可参考文档 https://blog.csdn.net/muumian123/article/details/81979084

求列表的差集-属于 A 列表,不属于 B 列表

方法一:
In [9]: def te():
...: a = set([1,2,3])
...: b = set([2,3,4])
...: print(list(a-b))
...: In [10]: te()
[1] 方法二:
In [11]: def te():
...: a = [1,2,3]
...: b = [2,3,0]
...: for i in a:
...: if i not in b:
...: print(i)
...: In [12]: te()
1

[每日一讲] Python系列:列表与元组的更多相关文章

  1. [每日一讲] Python系列:字典

    #! /usr/bin/python # coding:utf-8 """ DATA STRUCTURE Container: Mapping (Another cont ...

  2. [每日一讲] Python系列:字符串(下)

    字符串的常见操作 """ DATA STRUCTURE Container: Sequence -- String String is immutable.If stri ...

  3. [每日一讲] Python系列:Python概述

    Python 序章 概述 Python 是弱类型动态解释型的面向对象高级语言,其具备面向对象的三大特点:封装.继承.多态.Python 代码运行时,其有一个编译过程,通过编译器生成 .pyc 字节码 ...

  4. [每日一讲] Python系列:数字与运算符

    数字(数值)型 Python 数字数据类型用于存储数值.数据类型是不可变(immutable)的,这就意味着如果改变数字数据类型的值,将重新分配内存空间. Python 支持三种不同的数值类型: 整型 ...

  5. [每日一讲] Python系列:字符串(上)

    字符串作为人类最常处理的内容,在计算中决定了其占有重要的地位.在 Python 中,字符串的操作和处理往往需要根据实际问题,结合其他操作才可以完成目标.在复杂世界仅仅是字符串 API 还无法完成工作. ...

  6. Python基础------列表,元组的调用方法

    Python基础------列表,元组的调用方法@@@ 一. 列表 Python中的列表和歌曲列表类似,也是由一系列的按特定顺序排列的元素组成的,在内容上,可以将整数,实数,字符串,列表,元组等任何类 ...

  7. Python中列表,元组,字典,集合的区别

    参考文档https://blog.csdn.net/Yeoman92/article/details/56289287 理解Python中列表,元组,字典,集合的区别 列表,元组,字典,集合的区别是p ...

  8. Python基础-列表、元组、字典、字符串

    Python基础-列表.元组.字典.字符串   多维数组 nums1 = [1,2,3] #一维数组 nums2 = [1,2,3,[4,56]] #二维数组 nums3 = [1,2,3,4,['a ...

  9. Python:列表,元组

    一.列表 和字符串一样,列表也是序列类型,因此可以通过下标或者切片操作访问一个或者多个元素.但是,不一样的,列表是容器类型,是可以进行修改.更新的,即当我们进行修改列表元素,加入元素等操作的时候,是对 ...

随机推荐

  1. java:(设置编码集,密码的加密,JSTL,EL表达式,权限设置)

    1.设置编码集: package cn.zzsxt.lee.web.sevlet; import java.io.IOException; import javax.servlet.ServletEx ...

  2. Cmake + VS2013编译opencv

    下载之后,使用cmake转为vs解决方案的时候,配置OPENCV_EXTRA_MODULES_PATH=<path to opencv_contrib/modules/>,然后才能使用ex ...

  3. 自定义Spring-Boot @Enable注解

    Spring-Boot中有很多Enable开头的注解,通过添加注解来开启一项功能,如 其原理是什么?如何开发自己的Enable注解? 1.原理 以@EnableScheduling为例,查看其源码,发 ...

  4. springboot-elasticsearch项目启动报错:'elasticsearchTemplate' that could not be found

    解决: 将elasticsearch的相关配置加入到application.yml配置文件中就可以解决

  5. tensorflow各个版本的CUDA以及Cudnn版本对应关系

    概述,需要注意以下几个问题: (1)NVIDIA的显卡驱动程序和CUDA完全是两个不同的概念哦!CUDA是NVIDIA推出的用于自家GPU的并行计算框架,也就是说CUDA只能在NVIDIA的GPU上运 ...

  6. 【神经网络与深度学习】【Python开发】Caffe配置 windows下怎么安装protobuf for python

    首先从google上下载protobuf-2.5.0.zip和protoc-2.5.0-win32.zip,然后把protoc-2.5.0-win32.zip里的protoc.exe放到protobu ...

  7. 【VS开发】【图像处理】ISP图像传感器处理器基础

    1        前言 做为拍照手机的核心模块之一,camera sensor效果的调整,涉及到众多的参数,如果对基本的光学原理及sensor软/硬件对图像处理的原理能有深入的理解和把握的话,对我们的 ...

  8. Adobe出品(支持IOS,android,web调用)免费插件编辑图片

    <head runat="server"><meta http-equiv="Content-Type" content="text ...

  9. javaSE温习一&二

    这是一个简单的笔记 涉及到常量.变量:流程控制语句.数组:类与对象.封装.构造方法:Scanner类.Random类.Arraylist类: 1.pubic class  static void 2. ...

  10. LayUI 多选框动态加载、启用、禁用、赋值、取值等js实现

    例如多选框如下: <div class="layui-form"> <select xm-select="city"> </sel ...