[每日一讲] Python系列:列表与元组
参考文档 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系列:列表与元组的更多相关文章
- [每日一讲] Python系列:字典
#! /usr/bin/python # coding:utf-8 """ DATA STRUCTURE Container: Mapping (Another cont ...
- [每日一讲] Python系列:字符串(下)
字符串的常见操作 """ DATA STRUCTURE Container: Sequence -- String String is immutable.If stri ...
- [每日一讲] Python系列:Python概述
Python 序章 概述 Python 是弱类型动态解释型的面向对象高级语言,其具备面向对象的三大特点:封装.继承.多态.Python 代码运行时,其有一个编译过程,通过编译器生成 .pyc 字节码 ...
- [每日一讲] Python系列:数字与运算符
数字(数值)型 Python 数字数据类型用于存储数值.数据类型是不可变(immutable)的,这就意味着如果改变数字数据类型的值,将重新分配内存空间. Python 支持三种不同的数值类型: 整型 ...
- [每日一讲] Python系列:字符串(上)
字符串作为人类最常处理的内容,在计算中决定了其占有重要的地位.在 Python 中,字符串的操作和处理往往需要根据实际问题,结合其他操作才可以完成目标.在复杂世界仅仅是字符串 API 还无法完成工作. ...
- Python基础------列表,元组的调用方法
Python基础------列表,元组的调用方法@@@ 一. 列表 Python中的列表和歌曲列表类似,也是由一系列的按特定顺序排列的元素组成的,在内容上,可以将整数,实数,字符串,列表,元组等任何类 ...
- Python中列表,元组,字典,集合的区别
参考文档https://blog.csdn.net/Yeoman92/article/details/56289287 理解Python中列表,元组,字典,集合的区别 列表,元组,字典,集合的区别是p ...
- Python基础-列表、元组、字典、字符串
Python基础-列表.元组.字典.字符串 多维数组 nums1 = [1,2,3] #一维数组 nums2 = [1,2,3,[4,56]] #二维数组 nums3 = [1,2,3,4,['a ...
- Python:列表,元组
一.列表 和字符串一样,列表也是序列类型,因此可以通过下标或者切片操作访问一个或者多个元素.但是,不一样的,列表是容器类型,是可以进行修改.更新的,即当我们进行修改列表元素,加入元素等操作的时候,是对 ...
随机推荐
- ProxySQL 常见表配置
ProxySQL 常见表配置 [root@mgr1 opt]# rpm -ivh proxysql-1.4.14-1.1.el6.x86_64.rpm warning: proxysql-1.4.14 ...
- Python学习之表的介绍
9.4 表的介绍 存储引擎 数据的存储方式就是存储引擎,引擎不同,数据的存储方式就不同 MySQL中比较重要的引擎: InnoDB:mysql5.6以上,默认的存储方式 支持 transactio ...
- 安装opencv3.3.0碰到的问题及解决方法
出处:http://osask.cn/front/ask/view/258965 CMakeError.log Compilation failed: source file: '/home/jhro ...
- appium+python+安卓模拟器环境搭建和启动app实例
本文主要介绍在windows环境下搭建app自动化测试环境,具体步骤如下: 1.下载安卓sdk,网上很多资源 2.下载并安装安卓模拟器,官网上有 删除bin文件下的adb.exe和nox_adb.ex ...
- 【OpenCV开发】OpenCV:使用VideoCapture类进行视频读取和显示
原文来自:http://blog.csdn.net/thefutureisour/article/details/7530344 -------------以下为原文---------------这次 ...
- spring boot-9.对springMVC的支持
1.thymeleaf spring boot 推荐的模板引擎是thymeleaf.spring boot 的自动配置已经默认配置好了themleaf,只要导入themleaf的Starter就可以了 ...
- a++和++a的区别
a++是先执行表达式后再自增,执行表达式时使用的是a的原值.++a是先自增再执行表达示,执行表达式时使用的是自增后的a.例:int a=0printf("%d",a++); //输 ...
- py3 base64加密
import base64 #字符串编码: encodestr = base64.b64encode('abcr34r344r'.encode('utf-8')) print(str(encodest ...
- java程序员面试宝典1
1.在java中字符串只以Unicode一种形式存在(不选择任何特定的编码,直接使用他们在字符集中的编号,这是统一的唯一的方法) 2.在java中,是指在JVM中,在内存中,在你的代码里声明的每个ch ...
- 使用consul
eureka官方已经正式宣布:自2.0起不再维护该项目,并在github 项目wiki上放出了一段吓唬人的话: 大意就是:从2.x起,官方不会继续开发了,如果需要使用2.x,风险自负.但其实我觉得问题 ...