元组和列表的区别

  元组和列表几乎是一样的

  不一样的地方就是元组创建后元组的元素不可以修改,比如(添加,拓展,移除等修改功能,但是元组里的元素的元素是可以修改的)

基本操作:
  索引
  切片
  循环
  长度
  包含

创建元组

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. a = ("lyh", "guixiu", "xioum")
  4. #或者
  5. b = tuple(("lyh", "guixiu", "xioum"))

tuple转换元组
"""(转换成元组,需要转换的可迭代数据变量) 注意:能转换成元组的必须是可迭代的,也就是可以被for循环的"""
字符串,字典,列表 > 都可以转换成元组,转换成元组都是可以被for循环的,for循环每次循环到的数据就是元组的一个元素

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. a = "小鸡炖蘑菇"
  4. b = tuple(a)
  5. print(b)
  6. #输出 ('小', '鸡', '炖', '蘑', '菇')

索引

  格式:元组变量加[索引下标] 的方式

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. a = ("lyh", "guixiu", "xioum")
  4. print(a[1])
  5. #打印出 guixiu 打印出元素下标为1的元素

切片
  格式:元组变量加[起始下标:结束下标]

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. a = ("lyh", "guixiu", "xioum")
  4. print(a[0:3])
  5. #打印出 ('lyh', 'guixiu', 'xioum') 打印出元素下标0到3的元素

  len(p_object)

  """(统计元组里的元素数量)"""

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. a = ("lyh", "guixiu", "xioum")
  4. print(len(a))
  5. #打印出 3 统计出元组里有3个元素

  while循环

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. #while循环
  4. a = ("lyh", "guixiu", "xioum")
  5. b = 0
  6. while b < len(a):
  7. print(a[b])
  8. b += 1
  9. #循环出元组里的所有元素

  for循环

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. #for循环
  4. a = ("lyh", "guixiu", "xioum")
  5. for b in a: #b为自定义循环变量
  6. print(b)
  7. #循环出元组里的所有元素

  count(self, value)

  """(计算元素在元组里出现的次数)"""要计算的元素

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. a = ("lyh", "guixiu", "xioum")
  4. print(a.count("guixiu"))
  5. #打印出 1 guixiu元素在元组里出现一次

  index(self, value, start=None, stop=None)

  """(获取指定元素在元组里的索引位置)"""要查找的元素,起始位置,结束位置

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. a = ("lyh", "guixiu", "xioum")
  4. print(a.index("guixiu"))
  5. #打印出 1 guixiu元素在元组里的索引位置是1,默认从0开始计算
  1. lass tuple(object):
  2. """
  3. tuple() -> empty tuple
  4. tuple(iterable) -> tuple initialized from iterable's items
  5.  
  6. If the argument is a tuple, the return value is the same object.
  7. """
  8. def count(self, value): # real signature unknown; restored from __doc__
  9. """ T.count(value) -> integer -- return number of occurrences of value """
  10. return 0
  11.  
  12. def index(self, value, start=None, stop=None): # real signature unknown; restored from __doc__
  13. """
  14. T.index(value, [start, [stop]]) -> integer -- return first index of value.
  15. Raises ValueError if the value is not present.
  16. """
  17. return 0
  18.  
  19. def __add__(self, y): # real signature unknown; restored from __doc__
  20. """ x.__add__(y) <==> x+y """
  21. pass
  22.  
  23. def __contains__(self, y): # real signature unknown; restored from __doc__
  24. """ x.__contains__(y) <==> y in x """
  25. pass
  26.  
  27. def __eq__(self, y): # real signature unknown; restored from __doc__
  28. """ x.__eq__(y) <==> x==y """
  29. pass
  30.  
  31. def __getattribute__(self, name): # real signature unknown; restored from __doc__
  32. """ x.__getattribute__('name') <==> x.name """
  33. pass
  34.  
  35. def __getitem__(self, y): # real signature unknown; restored from __doc__
  36. """ x.__getitem__(y) <==> x[y] """
  37. pass
  38.  
  39. def __getnewargs__(self, *args, **kwargs): # real signature unknown
  40. pass
  41.  
  42. def __getslice__(self, i, j): # real signature unknown; restored from __doc__
  43. """
  44. x.__getslice__(i, j) <==> x[i:j]
  45.  
  46. Use of negative indices is not supported.
  47. """
  48. pass
  49.  
  50. def __ge__(self, y): # real signature unknown; restored from __doc__
  51. """ x.__ge__(y) <==> x>=y """
  52. pass
  53.  
  54. def __gt__(self, y): # real signature unknown; restored from __doc__
  55. """ x.__gt__(y) <==> x>y """
  56. pass
  57.  
  58. def __hash__(self): # real signature unknown; restored from __doc__
  59. """ x.__hash__() <==> hash(x) """
  60. pass
  61.  
  62. def __init__(self, seq=()): # known special case of tuple.__init__
  63. """
  64. tuple() -> empty tuple
  65. tuple(iterable) -> tuple initialized from iterable's items
  66.  
  67. If the argument is a tuple, the return value is the same object.
  68. # (copied from class doc)
  69. """
  70. pass
  71.  
  72. def __iter__(self): # real signature unknown; restored from __doc__
  73. """ x.__iter__() <==> iter(x) """
  74. pass
  75.  
  76. def __len__(self): # real signature unknown; restored from __doc__
  77. """ x.__len__() <==> len(x) """
  78. pass
  79.  
  80. def __le__(self, y): # real signature unknown; restored from __doc__
  81. """ x.__le__(y) <==> x<=y """
  82. pass
  83.  
  84. def __lt__(self, y): # real signature unknown; restored from __doc__
  85. """ x.__lt__(y) <==> x<y """
  86. pass
  87.  
  88. def __mul__(self, n): # real signature unknown; restored from __doc__
  89. """ x.__mul__(n) <==> x*n """
  90. pass
  91.  
  92. @staticmethod # known case of __new__
  93. def __new__(S, *more): # real signature unknown; restored from __doc__
  94. """ T.__new__(S, ...) -> a new object with type S, a subtype of T """
  95. pass
  96.  
  97. def __ne__(self, y): # real signature unknown; restored from __doc__
  98. """ x.__ne__(y) <==> x!=y """
  99. pass
  100.  
  101. def __repr__(self): # real signature unknown; restored from __doc__
  102. """ x.__repr__() <==> repr(x) """
  103. pass
  104.  
  105. def __rmul__(self, n): # real signature unknown; restored from __doc__
  106. """ x.__rmul__(n) <==> n*x """
  107. pass
  108.  
  109. def __sizeof__(self): # real signature unknown; restored from __doc__
  110. """ T.__sizeof__() -- size of T in memory, in bytes """
  111. pass

tuple

元组里的元素的元素追加
元组的元素是不可修改和和追加的,也就是元组的子级不可修改,元组的元素的元素也就是孙级是可以修改的

  1. #!/usr/bin/env python
  2. # -*- coding:utf-8 -*-
  3. #元组里的元素的元素追加
  4. #元组的元素是不可修改和和追加的,也就是元组的子级不可修改,元组的元素的元素也就是孙级是可以修改的
  5. #追加方式
  6. #列1
  7. a = (11,22,["guixiu",{"k1":"k2"}])
  8. b = {"k3":"k4"}
  9. a[2][1].update(b)#索引到元组里字典时,将b元组最佳进去
  10. print(a)
  11. #输出 (11, 22, ['guixiu', {'k1': 'k2', 'k3': 'k4'}])
  12.  
  13. #列2
  14. a = (11,22,["guixiu",{"k1":"k2"}])
  15. c = a[2][1]#索引到元组里的字典
  16. c["k3"] = "k4"
  17. print(a)
  18. #输出 (11, 22, ['guixiu', {'k1': 'k2', 'k3': 'k4'}])

元组的功能

转换列表
索引
切片
for循环
长度
反转
排序
索引位置
统计元素个数

第十五节,基本数据类型,元组tuple的更多相关文章

  1. 大白话5分钟带你走进人工智能-第十五节L1和L2正则几何解释和Ridge,Lasso,Elastic Net回归

    第十五节L1和L2正则几何解释和Ridge,Lasso,Elastic Net回归 上一节中我们讲解了L1和L2正则的概念,知道了L1和L2都会使不重要的维度权重下降得多,重要的维度权重下降得少,引入 ...

  2. 第四百一十五节,python常用排序算法学习

    第四百一十五节,python常用排序算法学习 常用排序 名称 复杂度 说明 备注 冒泡排序Bubble Sort O(N*N) 将待排序的元素看作是竖着排列的“气泡”,较小的元素比较轻,从而要往上浮 ...

  3. 第三百八十五节,Django+Xadmin打造上线标准的在线教育平台—登录功能实现,回填数据以及错误提示html

    第三百八十五节,Django+Xadmin打造上线标准的在线教育平台—登录功能实现 1,配置登录路由 from django.conf.urls import url, include # 导入dja ...

  4. 第三百七十五节,Django+Xadmin打造上线标准的在线教育平台—创建课程机构app,在models.py文件生成3张表,城市表、课程机构表、讲师表

    第三百七十五节,Django+Xadmin打造上线标准的在线教育平台—创建课程机构app,在models.py文件生成3张表,城市表.课程机构表.讲师表 创建名称为app_organization的课 ...

  5. 第三百六十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本查询

    第三百六十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—elasticsearch(搜索引擎)的基本查询 1.elasticsearch(搜索引擎)的查询 elasticsearch是功能 ...

  6. 第三百五十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy信号详解

    第三百五十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—scrapy信号详解 信号一般使用信号分发器dispatcher.connect(),来设置信号,和信号触发函数,当捕获到信号时执行 ...

  7. 第三百四十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫和反爬的对抗过程以及策略—scrapy架构源码分析图

    第三百四十五节,Python分布式爬虫打造搜索引擎Scrapy精讲—爬虫和反爬的对抗过程以及策略—scrapy架构源码分析图 1.基本概念 2.反爬虫的目的 3.爬虫和反爬的对抗过程以及策略 scra ...

  8. 第三百三十五节,web爬虫讲解2—Scrapy框架爬虫—豆瓣登录与利用打码接口实现自动识别验证码

    第三百三十五节,web爬虫讲解2—Scrapy框架爬虫—豆瓣登录与利用打码接口实现自动识别验证码 打码接口文件 # -*- coding: cp936 -*- import sys import os ...

  9. 第三百二十五节,web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签

    第三百二十五节,web爬虫,scrapy模块标签选择器下载图片,以及正则匹配标签 标签选择器对象 HtmlXPathSelector()创建标签选择器对象,参数接收response回调的html对象需 ...

  10. 第三百一十五节,Django框架,CSRF跨站请求伪造

    第三百一十五节,Django框架,CSRF跨站请求伪造  全局CSRF 如果要启用防止CSRF跨站请求伪造,就需要在中间件开启CSRF #中间件 MIDDLEWARE = [ 'django.midd ...

随机推荐

  1. HDU 5868 Different Circle Permutation(burnside 引理)

    HDU 5868 Different Circle Permutation(burnside 引理) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=586 ...

  2. react学习笔记-05 lifecycle

    根据React官网,react有三个生命状态:装载(Mounting),更新(updating),卸载() 一:装载 装载:componentWillMount/componentDidMount(组 ...

  3. python基础---pymsql

    pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 一.下载安装 pip3 install pymysql 二.使用 1.执行SQL #!/usr/bin/env ...

  4. VHDL设计问题

    在做算术运算的时候,不可以用std_ulogic_vector,必须是std_logic_vector.

  5. 关于C++中的重定位

    "标准库定义了4个IO对象,处理输入时使用命名为cin的istream类型对象,这个对象也成为标准输入.处理输出时使用命名为cout的ostream类型对象,这个对象也称为标准输出.标准库还 ...

  6. .NET架构师

    闲话不多扯,关于.NET架构师的培训  架构师的知识体系总结:7大重点,对7大重点作为细节的阐述将再后面陆续展开!架构师的体系纲领主要来着这7点.(必须严格记下) 1:现代软件开发过程及架构策略 1. ...

  7. PHP 5.4.8 添加系统服务命令

    之前没注意,PHP 5.4.8 的安装包有自带的系统服务注册文件的 打开编译安装包,换成你自己的路径 cd /mydata/soft/php-5.4.8/ cp sapi/fpm/init.d.php ...

  8. zoj 2193 poj 2585 Window Pains

    拓扑排序. 深刻体会:ACM比赛的精髓之处不在于学了某个算法或数据结构,而在于知道这个知识点但不知道这个问题可以用这个知识去解决!一看题目,根本想不到是拓扑排序.T_T...... #include& ...

  9. jquery远程班备忘

    基础第一课: 1. $(obj)获取的是一个集合,因此length最小是1, jquery,如果元素不存在,也不会报错,可通过$(obj).length<1就可以查看该元素是否存在. 2. at ...

  10. ckplayer 项目实战

    <div class="control-group" id="videoDiv" style="display: none;"> ...