参考文档 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. shell脚本判断端口是否打开

    [root@www zabbix_scripts]# cat check_httpd.sh #!/bin/bash a=`lsof -i: | wc -l` " ];then " ...

  2. jenkins 配置 gitlab webhook 实现自动发布

    测试环境需要git提交代码后,Jenkins自动部署,需要gitlab配置project webhook. 1,Jenkins版本2.89  gitlab 8.11 2,Jenkins需要安装插件:G ...

  3. 在webpack搭建的vue项目中如何管理好后台接口地址

    在最近做的vue项目中,使用了webpack打包工具,以前在做项目中测试环境和生产环境的接口地址都是一样的,由于现在接口地址不一样,需要在项目打包的时候手动切换不同的地址,有时候忘记切换就要重新打包, ...

  4. Multi-Object-Edit With Django FormSets

    I had to write a multi-object edit table the other day for a Django project and as such I dove into ...

  5. <数据结构系列3>队列的实现与变形(循环队列)

    数据结构第三课了,今天我们再介绍一种很常见的线性表——队列 就像它的名字,队列这种数据结构就如同生活中的排队一样,队首出队,队尾进队.以下一段是百度百科中对队列的解释: 队列是一种特殊的线性表,特殊之 ...

  6. python 进程和串行(——)

    进程 1.什么是串行? 串行:就是一个程序完完整整的运行完了,下个程序才运行. 2.什么是进程? 进程:一个正在运行的程序或一个程序运行的过程. 为什么要用进程. 提高程序执行效率. 多道技术:并发技 ...

  7. excel常用公式--时间序列类

    year,month,day:返回对应于某个日期的年月日.Year作为1900 - 9999之间的整数返回. weekday:返回对应于某个日期的一周中的第几天. WEEKNUM:返回特定日期的周数. ...

  8. Python小白需要知道的 20 个骚操作!

    Python小白需要知道的 20 个骚操作! Python 是一个解释型语言,可读性与易用性让它越来越热门.正如 Python 之禅中所述: 优美胜于丑陋,明了胜于晦涩. 在你的日常编码中,以下技巧可 ...

  9. JS获取指定范围随机数

    常用取整数的方法 : Math.floor(Math.random() * (max - min + 1)) + min 一步步来解析: Math.random() 函数返回一个浮点,  伪随机数在范 ...

  10. 解决jenkins的Console Output中文乱码

    1.本地机器设置环境变量(设置后需要注销计算机才能生效) key: JAVA_TOOL_OPTIONS value:-Dfile.encoding=UTF- 2. 通过Jenkins全局设置的方式   ...