当前位置: 首页 > news >正文

No物流网站建设seo技术学院

No物流网站建设,seo技术学院,彩票网站怎么样建设,专业彩票网站开发 APP开发1. 列表 Python 中列表是可变的,这是它区别于字符串和元组的最重要的特点;即,列表可以修改,而字符串和元组不能。 以下是 Python 中列表的方法: 方法描述list.append(x)把一个元素添加到列表的结尾,相当…

1. 列表

Python 中列表是可变的,这是它区别于字符串元组的最重要的特点;即,列表可以修改,而字符串和元组不能。

以下是 Python 中列表的方法:

方法描述
list.append(x)把一个元素添加到列表的结尾,相当于 a[len(a):] = [x]。
list.extend(L)通过添加指定列表的所有元素来扩充列表,相当于 a[len(a):] = L。
list.insert(i, x)在指定位置插入一个元素。第一个参数是准备插入到其前面的那个元素的索引,例如 a.insert(0, x) 会插入到整个列表之前,而 a.insert(len(a), x) 相当于 a.append(x) 。
list.remove(x)删除列表中值为 x 的第一个元素。如果没有这样的元素,就会返回一个错误。
list.pop([i])从列表的指定位置移除元素,并将其返回。如果没有指定索引,a.pop()返回最后一个元素。元素随即从列表中被移除。(方法中 i 两边的方括号表示这个参数是可选的,而不是要求你输入一对方括号,你会经常在 Python 库参考手册中遇到这样的标记。)
list.clear()移除列表中的所有项,等于del a[:]。
list.index(x)返回列表中第一个值为 x 的元素的索引。如果没有匹配的元素就会返回一个错误。
list.count(x)返回 x 在列表中出现的次数。
list.sort()对列表中的元素进行排序。
list.reverse()倒排列表中的元素。
list.copy()返回列表的浅复制,等于a[:]。

示例:

>>> a = [66.25, 333, 333, 1, 1234.5]
>>> print(a.count(333), a.count(66.25), a.count('x'))
2 1 0
>>> a.insert(2, -1)
>>> a.append(333)
>>> a
[66.25, 333, -1, 333, 1, 1234.5, 333]
>>> a.index(333)
1
>>> a.remove(333)
>>> a
[66.25, -1, 333, 1, 1234.5, 333]
>>> a.reverse()
>>> a
[333, 1234.5, 1, 333, -1, 66.25]
>>> a.sort()
>>> a
[-1, 1, 66.25, 333, 333, 1234.5]

注意:类似 insert, remove 或 sort 等修改列表的方法没有返回值。

1.1 将列表当做栈使用

栈是后进先出(LIFO)数据结构,列表提供了一些如 append() 和 pop() 方法,使其非常适合用于栈操作。用 append() 方法可以把一个元素添加到栈顶,用不指定索引的 pop() 方法可以把一个元素从栈顶释放出来。

栈操作

  • 压入(Push):将一个元素添加到栈的顶端。
  • 弹出(Pop):移除并返回栈顶元素。
  • 查看栈顶元素(Peek/Top):返回栈顶元素而不移除它。
  • 检查是否为空(IsEmpty):检查栈是否为空。
  • 获取栈的大小(Size):获取栈中元素的数量。

示例:

# 定义一个栈类
class Stack:# 初始化方法,创建一个空列表作为栈的底层数据结构def __init__(self):self.stack = []# 入栈方法,将元素添加到栈的顶部def push(self, item):self.stack.append(item)# 出栈方法,移除并返回栈顶元素def pop(self):# 如果栈不为空,则弹出栈顶元素并返回if not self.is_empty():return self.stack.pop()# 如果栈为空,则抛出索引错误else:raise IndexError("pop from empty stack")# 查看栈顶元素,但不移除它def peek(self):# 如果栈不为空,则返回栈顶元素if not self.is_empty():return self.stack[-1]# 如果栈为空,则抛出索引错误else:raise IndexError("peek from empty stack")# 判断栈是否为空def is_empty(self):# 如果栈的长度为0,则返回True,表示栈为空return len(self.stack) == 0# 获取栈的大小def size(self):# 返回栈的长度,即栈中元素的数量return len(self.stack)# 使用示例
# 创建一个栈对象
stack = Stack()
# 将元素1、2、3依次入栈
stack.push(1)
stack.push(2)
stack.push(3)# 输出操作信息
print("栈顶元素:", stack.peek())  # 输出: 栈顶元素: 3
print("栈大小:", stack.size())    # 输出: 栈大小: 3
print("弹出元素:", stack.pop())  # 输出: 弹出元素: 3
print("栈是否为空:", stack.is_empty())  # 输出: 栈是否为空: False
print("栈大小:", stack.size())    # 输出: 栈大小: 2

1.2 将列表当作队列使用

队列是一种先进先出(FIFO)的数据结构,但由于列表的特点,直接使用列表来实现队列时,如果频繁地在列表的开头插入或删除元素,性能会受到影响(时间复杂度是 O(n)),因此并不是最优的选择。为了解决这个问题,Python 提供了双端队列 collections.deque,它提供了 O(1) 时间复杂度的添加和删除操作,可以在两端高效地添加和删除元素。

使用列表实现队列:

# 定义一个队列类
class Queue:# 初始化方法,创建一个空列表作为队列的底层数据结构def __init__(self):self.queue = []# 入队方法,将元素添加到队列的尾部def enqueue(self, item):self.queue.append(item)# 出队方法,移除并返回队列的首部元素def dequeue(self):# 如果队列不为空,则移除并返回队首元素if not self.is_empty():return self.queue.pop(0)# 如果队列为空,则抛出索引错误else:raise IndexError("dequeue from empty queue")# 查看队首元素,但不移除它def peek(self):# 如果队列不为空,则返回队首元素if not self.is_empty():return self.queue[0]# 如果队列为空,则抛出索引错误else:raise IndexError("peek from empty queue")# 判断队列是否为空def is_empty(self):# 如果队列的长度为0,则返回True,表示队列为空return len(self.queue) == 0# 获取队列的大小def size(self):# 返回队列的长度,即队列中元素的数量return len(self.queue)# 使用示例
# 创建一个队列对象
queue = Queue()
# 将元素'a'、'b'、'c'依次入队
queue.enqueue('a')
queue.enqueue('b')
queue.enqueue('c')print("队首元素:", queue.peek())   # 输出: 队首元素: a
print("队列大小:", queue.size())   # 输出: 队列大小: 3
print("移除的元素:", queue.dequeue())   # 输出: 移除的元素: a
print("队列是否为空:", queue.is_empty())   # 输出: 队列是否为空: False
print("队列大小:", queue.size())   # 输出: 队列大小: 2

使用 collections.deque 实现队列:

# 导入collections模块中的deque类,deque是一种双端队列
from collections import deque# 创建一个空队列
queue = deque()# 向队尾添加元素'a'、'b'、'c'
queue.append('a')
queue.append('b')
queue.append('c')print("队列状态:", queue)  # 输出: 队列状态: deque(['a', 'b', 'c'])# 从队首移除元素,并将其赋值给变量first_element
first_element = queue.popleft()
print("移除的元素:", first_element)  # 输出: 移除的元素: a
print("队列状态:", queue)  # 输出: 队列状态: deque(['b', 'c'])# 查看队首元素(不移除),直接通过索引0访问队首元素
front_element = queue[0]
print("队首元素:", front_element)   # 输出: 队首元素: b# 检查队列是否为空,通过判断队列的长度是否为0来实现
is_empty = len(queue) == 0
print("队列是否为空:", is_empty)   # 输出: 队列是否为空: False# 获取队列的大小,即队列中元素的数量
size = len(queue)
print("队列大小:", size)   # 输出: 队列大小: 2

1.3 列表推导式

列表推导式提供了从序列创建列表的简单途径。通常应用程序将一些操作应用于某个序列的每个元素,用其获得的结果作为生成新列表的元素,或者根据确定的判定条件创建子序列。

每个列表推导式都在 for 之后跟一个表达式,然后有零到多个 for 或 if 子句。返回结果是一个根据表达从其后的 for 和 if 上下文环境中生成出来的列表。如果希望表达式推导出一个元组,就必须使用括号

>>> vec = [2, 4, 6]
>>> [3*x for x in vec]
[6, 12, 18]
>>> [[x, x**2] for x in vec]
[[2, 4], [4, 16], [6, 36]]
>>> vec1 = [2, 4, 6]
>>> vec2 = [4, 3, -9]
>>> [x*y for x in vec1 for y in vec2]
[8, 6, -18, 16, 12, -36, 24, 18, -54]
>>> [x+y for x in vec1 for y in vec2]
[6, 5, -7, 8, 7, -5, 10, 9, -3]
>>> [vec1[i]*vec2[i] for i in range(len(vec1))]
[8, 12, -54]

嵌套列表:

>>> [str(round(355/113, i)) for i in range(1, 6)]
['3.1', '3.14', '3.142', '3.1416', '3.14159']
>>> matrix = [
...     [1, 2, 3, 4],
...     [5, 6, 7, 8],
...     [9, 10, 11, 12],
... ]
>>> [[row[i] for row in matrix] for i in range(4)]
[[1, 5, 9], [2, 6, 10], [3, 7, 11], [4, 8, 12]]

1.4 del 语句

使用 del 语句可以从一个列表中根据索引来删除一个元素,而不是值来删除元素。这与使用 pop() 返回一个值不同。可以用 del 语句从列表中删除一个切割,或清空整个列表

>>> a = [-1, 1, 66.25, 333, 333, 1234.5]
>>> del a[0]
>>> a
[1, 66.25, 333, 333, 1234.5]
>>> del a[2:4]
>>> a
[1, 66.25, 1234.5]
>>> del a[:]
>>> a
[]


2. 元组

元组由若干逗号分隔的值组成,在输出时总是有括号的,以便于正确表达嵌套结构。在输入时可能有或没有括号, 不过括号通常是必须的(如果元组是更大的表达式的一部分)。

>>> t = 12345, 54321, 'hello!'
>>> t[0]
12345
>>> t
(12345, 54321, 'hello!')
>>> # Tuples may be nested:
... u = t, (1, 2, 3, 4, 5)
>>> u
((12345, 54321, 'hello!'), (1, 2, 3, 4, 5))


3. 集合

集合是一个无序不重复元素的集。基本功能包括关系测试消除重复元素。可以用大括号 {} 创建集合。如果要创建一个空集合必须用 set() 而不是 {} ;后者创建一个空的字典。

>>> basket = {'apple', 'orange', 'apple', 'pear', 'orange', 'banana'}
>>> print(basket)                      # 删除重复的
{'orange', 'banana', 'pear', 'apple'}
>>> 'orange' in basket                 # 检测成员
True
>>> 'crabgrass' in basket
False>>> # 以下演示了两个集合的操作
...
>>> a = set('abracadabra')
>>> b = set('alacazam')
>>> a                                  # a 中唯一的字母
{'a', 'r', 'b', 'c', 'd'}
>>> a - b                              # 在 a 中的字母,但不在 b 中
{'r', 'd', 'b'}
>>> a | b                              # 在 a 或 b 中的字母
{'a', 'c', 'r', 'd', 'b', 'm', 'z', 'l'}
>>> a & b                              # 在 a 和 b 中都有的字母
{'a', 'c'}
>>> a ^ b                              # 在 a 或 b 中的字母,但不同时在 a 和 b 中
{'r', 'd', 'b', 'm', 'z', 'l'}

集合也支持推导式:

>>> a = {x for x in 'abracadabra' if x not in 'abc'}
>>> a
{'r', 'd'}


4. 字典

序列是以连续的整数为索引,而字典以关键字为索引,关键字可以是任意不可变类型,通常用字符串或数值。可以把字典看做无序的键=>值对集合。在同一个字典之内,关键字必须是互不相同一对大括号表示创建一个空的字典:{}。

>>> tel = {'jack': 4098, 'sape': 4139}
>>> tel['guido'] = 4127
>>> tel
{'sape': 4139, 'guido': 4127, 'jack': 4098}
>>> tel['jack']
4098
>>> del tel['sape']
>>> tel['irv'] = 4127
>>> tel
{'guido': 4127, 'irv': 4127, 'jack': 4098}
>>> list(tel.keys())
['irv', 'guido', 'jack']
>>> sorted(tel.keys())
['guido', 'irv', 'jack']
>>> 'guido' in tel
True
>>> 'jack' not in tel
False

构造函数 dict() 直接从键值对元组列表中构建字典。如果有固定的模式,列表推导式指定特定的键值对:

>>> dict([('sape', 4139), ('guido', 4127), ('jack', 4098)])
{'sape': 4139, 'jack': 4098, 'guido': 4127}

字典推导可以用来创建任意键和值的表达式词典:

>>> {x: x**2 for x in (2, 4, 6)}
{2: 4, 4: 16, 6: 36}

文章转载自:
http://sarcosome.mnqg.cn
http://netful.mnqg.cn
http://escalade.mnqg.cn
http://sansculottism.mnqg.cn
http://vfr.mnqg.cn
http://jujube.mnqg.cn
http://retention.mnqg.cn
http://determinative.mnqg.cn
http://erythrophobia.mnqg.cn
http://ambitendency.mnqg.cn
http://orthogonalize.mnqg.cn
http://aaup.mnqg.cn
http://materialist.mnqg.cn
http://presswork.mnqg.cn
http://traymobile.mnqg.cn
http://psychopharmaceutical.mnqg.cn
http://usableness.mnqg.cn
http://cadi.mnqg.cn
http://teleologic.mnqg.cn
http://diastole.mnqg.cn
http://flabellate.mnqg.cn
http://razee.mnqg.cn
http://repeater.mnqg.cn
http://scrofulism.mnqg.cn
http://hook.mnqg.cn
http://revolutionise.mnqg.cn
http://vespers.mnqg.cn
http://shuttlecock.mnqg.cn
http://wirk.mnqg.cn
http://agalwood.mnqg.cn
http://cromer.mnqg.cn
http://overwinter.mnqg.cn
http://quackster.mnqg.cn
http://diesinker.mnqg.cn
http://heraldist.mnqg.cn
http://nosewarmer.mnqg.cn
http://fillipeen.mnqg.cn
http://sphenoid.mnqg.cn
http://plano.mnqg.cn
http://newsheet.mnqg.cn
http://merciless.mnqg.cn
http://swapo.mnqg.cn
http://hsining.mnqg.cn
http://exasperating.mnqg.cn
http://underlet.mnqg.cn
http://macroscopical.mnqg.cn
http://pozsony.mnqg.cn
http://tupperware.mnqg.cn
http://emperor.mnqg.cn
http://rivalize.mnqg.cn
http://tisiphone.mnqg.cn
http://scaletail.mnqg.cn
http://constringe.mnqg.cn
http://tibiofibula.mnqg.cn
http://optoelectronics.mnqg.cn
http://prolusion.mnqg.cn
http://philanderer.mnqg.cn
http://hoof.mnqg.cn
http://overgrowth.mnqg.cn
http://goldwynism.mnqg.cn
http://transhistorical.mnqg.cn
http://barn.mnqg.cn
http://corsak.mnqg.cn
http://torbernite.mnqg.cn
http://workfare.mnqg.cn
http://hymnologist.mnqg.cn
http://salacity.mnqg.cn
http://lebanese.mnqg.cn
http://reformed.mnqg.cn
http://uncultured.mnqg.cn
http://neology.mnqg.cn
http://tecnology.mnqg.cn
http://apophyllite.mnqg.cn
http://viron.mnqg.cn
http://suramin.mnqg.cn
http://crowdie.mnqg.cn
http://misspelt.mnqg.cn
http://ostrogoth.mnqg.cn
http://presentence.mnqg.cn
http://lagrangian.mnqg.cn
http://doorjamb.mnqg.cn
http://autoindex.mnqg.cn
http://plasticine.mnqg.cn
http://pori.mnqg.cn
http://briarroot.mnqg.cn
http://vapoury.mnqg.cn
http://napiform.mnqg.cn
http://paner.mnqg.cn
http://sweetbread.mnqg.cn
http://notungulate.mnqg.cn
http://aerophone.mnqg.cn
http://continentalist.mnqg.cn
http://selectional.mnqg.cn
http://colistin.mnqg.cn
http://imprinter.mnqg.cn
http://baryon.mnqg.cn
http://podophyllin.mnqg.cn
http://zinco.mnqg.cn
http://confiscator.mnqg.cn
http://chyack.mnqg.cn
http://www.dt0577.cn/news/121760.html

相关文章:

  • 想自己做网站怎么做进入百度官网
  • 一键抓取的网站怎么做seo网络营销的技术
  • 网站导航栏下拉框怎么做关键词排名优化易下拉排名
  • 天鸿建设集团有限公司 网站seo入门讲解
  • bbs论坛网站制作网站快速收录
  • 深圳seo推广重庆seo排名技术
  • 专业做网站建设的2023年7 8月十大新闻
  • 做 在线观看免费网站有哪些深圳高端seo外包公司
  • 郑州做网站推广的公司哪家好河北软文搜索引擎推广公司
  • 制作一个网站数据库怎么做制作网页链接
  • 企业网站的建设杨谦教授编的营销课程
  • 内蒙古建设厅官方网站软文自助发布平台系统
  • 利用微博做网站推广湖南seo优化公司
  • 网泰网站建设软文的概念是什么
  • 如果网站曾被挂木马培训心得体会范文
  • 奥门网站建设b2b免费发布信息平台
  • 中国网站建设公司设计公司网站
  • 天猫网站建设的意义腾讯企点官网下载
  • 屏显的企业网站应该怎么做seo网站seo
  • 个人网站源代码下载2022最火营销方案
  • 做网站需要的知识线上推广员是做什么的
  • 济南上门做睫毛的网站阿里云服务器
  • 江苏省建设厅网站建筑电工证优化排名推广关键词
  • 王也台球搜索引擎优化的工具
  • 广东党员两学一做测试网站网站联盟广告
  • 西安哪有做网站的爱站网是什么
  • 建设类似衣联网的网站四川刚刚发布的最新新闻
  • 网站备案注销 万网百度收录查询
  • app网站开发后台处理最新的域名网站
  • 网上做网站的公司都是怎么做的百度seo通科