[每日一讲] 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:列表,元组
一.列表 和字符串一样,列表也是序列类型,因此可以通过下标或者切片操作访问一个或者多个元素.但是,不一样的,列表是容器类型,是可以进行修改.更新的,即当我们进行修改列表元素,加入元素等操作的时候,是对 ...
随机推荐
- sql server 2014安装后用sa登录问题
在使用的sql server的数据的情况下,安装数据过程,未指定使用sa的登录,只能使用windows的账户登录,那要怎么设置账户来使用sa账户登录账号呢? 首先先打开的是sql server man ...
- 借助marquee实现弹幕效果
HTML标签marquee实现滚动效果 .基于此,实现简易版 弹幕: HTML <div class="right_liuyan"> <marquee id=& ...
- C语言作业Ⅰ12
一.我学到的内容 二.我的收获 时间 作业链接 我的收获 第一周 https://www.cnblogs.com/deng9/p/11576196.html#4369234 让我对这个专业有了新的认识 ...
- hue的load balance
参考: hue的load balance官网: https://www.cloudera.com/documentation/enterprise/6/6.2/topics/hue_use_add_l ...
- 【基本优化实践】【1.4】tempdb优化
[1]tempdb介绍 tempdb全局存储内部对象,用户对象,临时表,临时对象,以及SQL Server操作创建的存储过程.每个数据库实例只有一个tempdb,所以可能存在性能以及磁盘空间瓶颈. 各 ...
- 关于float的小奥秘
一. float 存储方式 1.1. float 占四个字节 1.2. 浮点数构成 1.2.1. 无论是单精度还是双精度在存储中都分为三个部分: <1>. 符号位(Sign) : 0代表正 ...
- CF 1136C Nastya Is Transposing Matrices
题目链接:http://codeforces.com/problemset/problem/1136/C 题目分析 看了题目之后,第一想法:任意位置都可以倒置,要是枚举,铁定的超时.所以需要探索规律. ...
- AtCoder Beginner Contest 072
这应该是我第二次打AtCoder, 题目其实并不难,就是自己经验不足想复杂了,再加上自己很笨,愣是做了97分钟才全做出来(最后三分钟,有点小激动..),看着前面大牛半个小时都搞完了,真心膜拜一下,代码 ...
- linux:RAID(磁盘阵列)笔记
RAID磁盘阵列简述: RAID0(条带): 把多个同样大小的磁盘串联起来当做一个磁盘来用. 优点:读写速度快. 缺点:数据容易丢失(没有容错能力). ...
- eclipse经常弹出Subversion Native Library Not Available解决方案