首页 > 代码库 > review what i studied `date` - 2017-4-9
review what i studied `date` - 2017-4-9
#!/bin/python #!_*_conding:UTF-8_*_ import os import json r=open(‘url.txt‘,‘r‘).read().split() #读取url.txt文件返回数组 u=open(‘user.txt‘,‘r‘).read().split() devices=[] #定义devices类型为数组list users=[] a = 0 for devpath in r: user = u[a] device = os.path.basename(devpath) #取斜杠后边的值例如 再经过basename 转换后会成为xuhui.com #str devices += [{‘{#SITENAME}‘:device,‘{#user}‘:‘user‘}] #定义数据 #devices的类型从list 变为dict a+=1 print json.dumps({‘data‘:devices},sort_keys=True,indent=7,separators=(‘,‘,‘:‘)) #格式化数据 #sort_keys sort #print devices
Python set 集合
集合内的元素不能重叠
常用集合:
>>> b = set(‘c‘) >>> b.update(‘de‘) ;添加d,e >>> b.add(‘python‘) ;添加python >>> b.add(‘f‘) >>> b.remove(‘python‘) >>> a = set(‘abc‘) >>> b = set(‘cdef‘) >>> a & b ;a和b的交集 set([‘c‘]) >>> a | b set([‘a‘, ‘c‘, ‘b‘, ‘e‘, ‘d‘, ‘f‘]) >>> b -a set([‘e‘, ‘d‘, ‘f‘]) >>> b - a set([‘e‘, ‘d‘, ‘f‘])
Python dict
adict = {key1:value2, key2:value2, …}
>>> a = {"name":"xuhui","address":"haidian"} >>> a {‘name‘: ‘xuhui‘, ‘address‘: ‘haidian‘} >>> type(a) <type ‘dict‘> #有点类似于json #缺点:key是唯一的 >>> a {‘name‘: ‘xuhui‘, ‘address‘: ‘haidian‘} >>> a = {"name":"xuhui","address":"haidian","name":"xuyue"} >>> a {‘name‘: ‘xuyue‘, ‘address‘: ‘haidian‘} #上边的name:xuyue并没有被添加而是替代了key原来的值 >>> a.get(‘name‘) ‘xuyue‘ #查字典 >>> for key in a.keys():print key ... name address #遍历字典键值 >>> for value in a.values():print value ... xuyue haidian #遍历字典的键值的值 >>> for item in a.items():print item ... (‘name‘, ‘xuyue‘) (‘address‘, ‘haidian‘) #遍历字典的项(元素) >>> for key,value in a.items(): print ‘key=%s, value=http://www.mamicode.com/%s‘ %(key,value)>#VMware ESX 运行在裸机上,可以说他是一个操作系统
#用VMware Sphere Client连接的那个
review what i studied `date` - 2017-4-9
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。