摘要:本文主要向大家介绍了Python语言元组 字典 集合,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
本文主要向大家介绍了Python语言元组 字典 集合,通过具体的内容向大家展示,希望对大家学习Python语言有所帮助。
1.列表构建栈的数据结构:
栈的特点:先进后出
测试结果:
2.列表构建队列的数据结构:
队列的特点:先进先出
3.Is和等于号的区别:
字符串驻留机制:
对于较小的字符串,id相同
对于较长的字符串,id不相同,因为不会驻留字符串的副本。
注意:在进行测试时,一定要在交互式环境测试。
测试:
In [2]: b = 'hello'
In [3]: print id(a),id(b)
40886560 40886560
In [4]: c = 'hello java world'
In [5]: d = 'hello java world'
In [6]: print id(c), id(d)
40923296 40923464
In [7]: print c is d
False
In [8]: e = 'python'
In [9]: f = "".join(['p', 'y', 't', 'h', 'o', 'n'])
In [10]: print id(e), id(f)
140309747759888 40886608
In [11]: li = [1, 2, 3]
In [12]: li1 = li
In [13]: id(li)
Out[13]: 40893688
In [14]: id(li1)
Out[14]: 40893688
In [15]: li1 = li[:]
In [16]: id(li), id(li1)
Out[16]: (40893688, 40891672)
copy.copy()
In [17]: li = ['fentiao', 'zhurou', ['fensi', 'fendai']]
In [18]: li1 = li[:]
In [19]: id(li), id(li1)
Out[19]: (40891600, 40878592)
In [20]: id(li[-1]), id(li[-1])
Out[20]: (40906264, 40906264)
In [21]: import copy
In [22]: li2 = copy.copy(li)
In [23]: id(li), id(li1), id(li2)
Out[23]: (40891600, 40878592, 40865016)
In [24]: id(li[-1]), id(li1[-1]), id(li2[-1])
Out[24]: (40906264, 40906264, 40906264)
In [25]: li3 = copy.deepcopy(li)
In [26]: id(li[-1]), id(li1[-1]), id(li3[-1])
Out[26]: (40906264, 40906264, 40879960)
In [27]: t = (1, 1.0, 2j, True, (1,2,3))
In [28]: print t
(1, 1.0, 2j, True, (1, 2, 3))
In [29]: t1 = (1,)
In [30]: print type(t1)
<type 'tuple'>
In [31]: t = tuple()
In [32]: print type(t)
<type 'tuple'>
In [34]: print t[0], t[-1], t[-1][-1]
1 [1, 2] 2
In [35]: print t[::-1]
([1, 2], 'hello', (1+2j), 1L, 1.0, 1)
In [36]: print t+(1,2,3)
(1, 1.0, 1L, (1+2j), 'hello', [1, 2], 1, 2, 3)
In [37]: print t * 3
(1, 1.0, 1L, (1+2j), 'hello', [1, 2], 1, 1.0, 1L, (1+2j), 'hello', [1, 2], 1, 1.0, 1L, (1+2j), 'hello', [1, 2])
In [38]: print 1 in t, 1 not in t
True False
In [41]: allow_ips = ('172.25.254.1', '172.25.254.12', '172.25.254.13')
In [42]: for ip in allow_ips:
....: print ip
....:
172.25.254.1
172.25.254.12
172.25.254.13
ips = []
for i in range(1, 255):
ip = '172.25.254.'+str(i)
ips.append('172.25.254.' + str(i))ports = (21, 22, 80, 3306, 3020)
for ip in ips:
for port in ports:
print '[+] Scanning %s:%d' % (ip, port)
In [43]: t.count(1)
Out[43]: 3
In [44]: t.index(1)
Out[44]: 0
d = {
:前面的称为键,key
#:后面的称为值,value#键值对(key-value)'name': 'root','passwd':'westos'}
print d['name']
print d['passwd']
info = {
'root':{
'name': 'root',
'passwd':'westos',
'age':18,
'eamil':['westos@qq.com', 'redhat@qq.com']
},
}
print info['root']
通过工厂函数创建字典d = dict()
print type(d)
d = dict(a=1, b=2, c=3)
print d, type(d)
cardids = []
for i in range(1, 1001):
cardid = "61021%.4d" % (i)
cardids.append((cardid))
cardInfo = {}.fromkeys(cardids, '666666')
#print len(cardInfo)
for i, j in enumerate(cardInfo):
In [1]: d = dict(a=1, b=2)
In [2]: d
Out[2]: {'a': 1, 'b': 2}
In [3]: d.update(c=5,d=6)
In [4]: d
Out[4]: {'a': 1, 'b': 2, 'c': 5, 'd': 6}
In [5]: d.update(a=10,d=100,f=9)
In [6]: d
Out[6]: {'a': 10, 'b': 2, 'c': 5, 'd': 100, 'f': 9}
In [1]: d = dict(a=1, b= 2)
In [2]: d.setdefault('a', 10)
Out[2]: 1
In [3]: d
Out[3]: {'a': 1, 'b': 2}
In [4]: d.setdefault('f', 10)
Out[4]: 10
In [5]: d
Out[5]: {'a': 1, 'b': 2, 'f': 10}
In [6]: d.keys() #查询key值
Out[6]: ['a', 'b', 'f']
In [7]: d.values() #查询values值
Out[7]: [1, 2, 10]
In [8]: d.items() #查询键值对
Out[8]: [('a', 1), ('b', 2), ('f', 10)]
In [9]: for i,j in d.items():
...: print i,j
...:
a 1
b 2
f 10
In [10]: d.has_key('a') #查询字典里是否含有‘a’这个key值
Out[10]: True
In [11]: d
Out[11]: {'a': 1, 'b': 2, 'f': 10}
In [12]: d.pop('e', 1)
Out[12]: 1
In [13]: d.pop('a')
Out[13]: 1
In [14]: d
Out[14]: {'b': 2, 'f': 10}
In [15]: d.pop('b', 10)
Out[15]: 2
In [19]: d
Out[19]: {'a': 1, 'b': 2, 'c': 3, 'f': 10}
In [20]: d.popitem()
Out[20]: ('a', 1)
In [21]: d
Out[21]: {'b': 2, 'c': 3, 'f': 10}
In [22]: del d['c']
In [23]: d
Out[23]: {'b': 2, 'f': 10}
In [24]: del d['c']
KeyError Traceback (most recent call last)
<ipython-input-24-975cd7d7076f> in <module>()
----> 1 del d['c']
KeyError: 'c'
In [34]: d.clear() #删除字典里所有元素
In [35]: d
Out[35]: {}
In [36]: del d #删除整个字典
利用if语句实现switch(实现四则运算)#!/usr/bin/env python
#coding:utf-8
from future import division
while 1:
num1 = input('Num1:')
oper = raw_input('操作符:')
num2 = input('Num2:')
#!/usr/bin/env python
#coding:utf-8
from future import division
num1 = input('Num1:')
oper = raw_input('操作符:')
num2 = input('Num2:')
def add(num1, num2):
return num1 + num2
def div(num1, num2):
if num2 == 0:
raise IOError
else:
return num1 / num2
d = {
'+': add,
'-': num1 - num2,
'': num1 num2,
'/': div,
}
if oper in d:
print d[oper](num1, num2)
else:
print 'error'
#!/usr/bin/env python
#coding:utf-8
favourite_places = {
'lee': ['xian', 'hangzhou'],
'fentiao':['hanzhong', 'xianyang']
}
for name in favourite_places:
print "\n" + name.title() + "'s favourite place are:"
for place in favourite_places[name]:
print place
本文由职坐标整理并发布,希望对同学们学习Python有所帮助,更多内容请关注职坐标编程语言Python频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号