博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
元组、字典、序列、对象与参考
阅读量:6902 次
发布时间:2019-06-27

本文共 2754 字,大约阅读时间需要 9 分钟。

例一:

元组:不可变,圆括号里面的逗号

和列表十分类似,只不过元组和字符串一样是 不可变的 即你不能修改元组,元组通过圆括号中用逗号分割的项目定义。元组通常用在使语句或用户定义的函数能够安全地采用一组值的时候,即被使用的元组的值不会改变。

#!/usr/bin/python# Filename: using_tuple.pyzoo = ('wolf', 'elephant', 'penguin')print 'Number of animals in the zoo is', len(zoo)new_zoo = ('monkey', 'dolphin', zoo)print 'Number of animals in the new zoo is', len(new_zoo)print 'All animals in new zoo are', new_zooprint 'Animals brought from old zoo are', new_zoo[2]print 'Last animal brought from old zoo is', new_zoo[2][2]

例二:

字典:键值对。d = {key1 : value1, key2 : value2 }

字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把(名字)和(详细情况)联系在一起。注意,键必须是唯一的

#-*-encoding:utf-8-*-#!/usr/bin/env python# Filename: using_dict.py# 字典类似于你通过联系人名字查找地址和联系人详细情况的地址簿,即,我们把键(名字)和值(详细情况)联系在一起。#注意,键必须是唯一的,就像如果有两个人恰巧同名的话,你无法找到正确的信息ab={    'Swaroop'    : 'swaroopch@byteofpython.info',    'Larry'        : 'larry@wall.org',    'Matsumoto'    : 'matz@ruby-lang.org',    'Spammer'    : 'spammer@hotmail.com'   }print "Swaroop's address is %s" %ab['Swaroop']# Adding a key/value pairab['Guido']='guido@python.org'# Deleting a key/value pairdel ab['Spammer']print '\nThere are %d contacts in the address-book\n' %len(ab)for name,address in ab.items():    print 'Contact %s at %s' %(name,address)if 'Guido' in ab: # OR ab.has_key('Guido')    print "\nGuido's address is %s" %ab['Guido']

例三、序列

列表、元组和字符串都是序列,但是序列是什么,它们为什么如此特别呢?序列的两个主要特点是索引操作符和切片操作符。

#!/usr/bin/env python# Filename: seq.pyshoplist=['apple','mango','carrot','banana']# Indexing or 'Subscription' operationprint 'Item 0 is', shoplist[0]print 'Item 1 is', shoplist[1]print 'Item 2 is', shoplist[2]print 'Item 3 is', shoplist[3]print 'Item -1 is', shoplist[-1]print 'Item -2 is', shoplist[-2]# Slicing on a listprint 'Item 1 to 3 is', shoplist[1:3]print 'Item 2 to end is', shoplist[2:]print 'Item 1 to -1 is', shoplist[1:-1]print 'Item start to end is', shoplist[:]# Slicing on a stringname='swaroop'print 'characters 1 to 3 is', name[1:3]print 'characters 2 to end is', name[2:]print 'characters 1 to -1 is', name[1:-1]print 'characters start to end is', name[:]

例四 参考:(指向)

#!/usr/bin/env python# Filename: reference.pyprint 'Simple Assignment'shoplist=['apple','mango','carrot','banana']mylist=shoplist # mylist is just another name pointing to the same object!del shoplist[0]  #del mylist[0] 也是一样的结果 print 'shoplist is',shoplistprint 'mylist is',mylist# notice that both shoplist and mylist both print the same list without# the 'apple' confirming that they point to the same objectprint 'Copy by making a full slice'mylist=shoplist[:] # make a copy by doing a full slicedel mylist[0] # remove first itemprint 'shoplist is', shoplistprint 'mylist is', mylist# notice that now the two lists are different

 

转载于:https://www.cnblogs.com/bluewelkin/p/4397486.html

你可能感兴趣的文章
青蛙的约会(扩展欧几里得)
查看>>
Asia Yokohama Regional Contest 2018 C题 - Arithmetic Progressions(思维)
查看>>
UVa 101 - The Blocks Problem STL
查看>>
计算机专业术语
查看>>
Leetcode-探索 | 移动零
查看>>
DBI 数据库模块剖析:Perl DBI 数据库通讯模块规范,工作原理和实例
查看>>
Tesseract+opencv+VS+win实现OCR
查看>>
android在activity中锁屏解锁后重走OnCreate的问题的解决办法
查看>>
[学习笔记]博弈论
查看>>
python基础:搜索路径
查看>>
python os sys模块(二)
查看>>
一次linux启动故障记录
查看>>
linux 3.10内核 xfs的一次io异常导致的hung crash
查看>>
Castle ActiveRecord学习笔记(转)
查看>>
change textblock background color when text equal to referenceValue
查看>>
springboot+mybatis环境的坑和sql语句简化技巧
查看>>
如何用oracle从身份证信息中提取出生日期?
查看>>
Keil C编译器的变量存储分配
查看>>
非常不错的js 屏蔽类加验证类
查看>>
Innodb间隙锁,细节讲解(转)
查看>>