讲述Python序列如何进行解包教程

开发 后端
Python序列如何来完成相关的工作呢?这个问题有些时候会困扰我们。在下面我们就来详细的看下如何进行相关的知识应用。

Python序列具有很广泛的应用范围,在实际的应用中还是有不少的问题需要我们大家解决。下面我们就来看看相关的问题如何进行解决。希望在今后的工作中有所帮助。#t#

Python序列(字符串,列表,元组)

Python序列的***个元素从0开始,它不但可以从头开始访问,也可以从尾部访问,***一个元素是a[-1],倒数第二个是a[-2],倒数第i个是a[-i].

列表的创建,遍历,修改等,列表中可以存储不同类型的元素,习惯上都使用列表存储通类型的数据。长度可以在运行时修改。

 

元组,通常存储异种数据的序列,这个也是习惯,非规则。长度事先确定的,不可以在程序执行期间更改。元组的创建可以访问:

  1. aList = []for number in range( 1, 11 ): aList += [ number ]
    print "The value of aList is:", aList for item in aList: print item,
    print for i in range( len( aList ) ): print "%9d %7d" % 
    ( i, aList[ i ] )aList[ 0 ] = -100 aList[ -3 ] = 19print "Value of aList after modification:", aList   
  2. 7.3.  
  3. hour = 2 
  4. minute = 12 
  5. second = 34 
  6. currentTime = hour, minute, second # create tuple  
  7. print "The value of currentTime is:", currentTime  

 

Python序列解包

atupe=(1,2,3)来创建元组,称为”元组打包”,因为值被“打包到元组中”,元组和其他序列可以“解包”即将序列中存储的值指派给各个标识符。例子:


  1. # create 
    sequencesaString = "abc"aList = [ 1, 2, 3 ]
    aTuple = "a", "A",   
  2. # unpack sequences to variablesprint "Unpacking string..."
    first, second, 
    third = aStringprint "String values:", first,
     second, third print "\nUnpacking list..."first, second, 
    third = aListprint "List values:", first, second, third 
    print "\nUnpacking tuple..."first, second, 
    third = aTupleprint 
    "Tuple values:", first, second, third   
  3. # swapping two valuesx = 3y = 4 print "\nBefore 
    swapping: 
    x = %d, y = %d" % ( x, y )x, yy = y, x # swap varia
    blesprint "After swapping: 
    x = %d, y = %d" % ( x, y )  

以上就是对Python序列的相关介绍。希望对大家有所帮助。

责任编辑:张浩 来源: CSDN
相关推荐

2010-03-12 20:08:53

Python统计

2010-02-03 13:55:51

Python 代码

2010-02-22 16:05:40

Python配置

2010-02-01 10:21:36

Python编码转换

2010-02-01 18:20:17

Python 多重继承

2010-03-11 17:38:20

Python中文

2010-02-02 16:47:12

Python主线程

2010-03-15 15:11:50

Python列表

2010-02-01 14:48:43

2010-02-01 17:33:24

Python主线程

2010-02-01 16:22:36

Python字符串操作

2010-03-15 15:45:15

Python编程语言

2010-03-01 11:06:52

Python 调试器

2010-02-22 17:12:34

Python对象

2010-03-01 16:32:36

Python语言

2010-03-09 18:14:55

Python sock

2010-03-10 19:34:45

Python主线程

2021-05-24 12:10:54

PythonSpaceX代码

2010-03-12 19:29:15

python svn脚

2010-03-12 17:35:00

Python字符串
点赞
收藏

51CTO技术栈公众号