参考文档 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

最新文章

  1. LINQ、Lambda 的转换
  2. 使用SDWebImage下载图片,sharedDownloader方法下载成功,new 方法下载失败
  3. UML从需求到实现----用例
  4. Office 2013 note
  5. notepad++插件使用说明
  6. CycleScrollView实现轮播图
  7. [LeetCode] Longest Valid Parentheses 解题思路
  8. Bootstrap3 栅格系统-实例:流式布局容器
  9. Transact-SQL解析和基本的实用语句
  10. The CLI moved into a separate package: webpack-cli.解决办法
  11. POJ 2456 3258 3273 3104 3045(二分搜索-最大化最小值)
  12. Hadoop2源码分析-RPC机制初识
  13. 关于limit_req和limit_conn的区别
  14. C#汉字转拼音,可识别多音字,带声调,提供正向、逆向、双向分词算法的小程序
  15. 自制精排 ePub 集、不定期更新(UPDATA-2015-8-2)
  16. BZOJ5073 小A的咒语(动态规划)
  17. Nginx 内嵌lua脚本,结合Redis使用
  18. .netCore2.0 配置文件
  19. Linux GPIO键盘驱动开发记录_OMAPL138
  20. DFS——hdu5682zxa and leaf

热门文章

  1. FreeBSD Here is how to upgrade
  2. spring cloud依赖服务调用优化
  3. js中的break,continue和return的用法及区别
  4. python学习道路即将结束
  5. C#学习笔记一(概念,对象与类型,继承)
  6. Cocos2d-X多线程(3) cocos2dx中的线程安全
  7. PTA(Basic Level)1031.查验身份证
  8. springboot - 应用实践(N)使用springboot内置的@Scheduled
  9. MD5算法+盐Salt
  10. nginx服务报403错误的解决方法