`
venceinfo
  • 浏览: 38266 次
  • 性别: Icon_minigender_1
  • 来自: 南京
社区版块
存档分类
最新评论

基础(2)

阅读更多
//未定义变量,操作报异常
try:
	li
	if len(li) == 0:
		print 'ok'
except:
	print 'fail'
-------------------------------------------------------------------------------------------
//输出格式
k = "uid"
v = "sa"
print "%s=%s" % (k,v)

-------------------------------------------------------------------------------------------
infoDir = {"Shool":"BeiJing,ShangHai,JinagSu",\
           "Home":"Nam,Jan,MM",\
           "Host":"XX"}

for k,v in infoDir.items():
	valueList = v.split(",")
	for value in valueList:
		print value

-------------------------------------------------------------------------------------------
//dir的值为dir型
dir = {"name":{"xx":"yy"}}

//强制类型转换
tempDir = dict(dir["name"])

print tempDir["xx"]

//删除一个值
del dir["database"]
//清空
dir.clear()

dir1 = {"name":"xuxu","age":23}
//增加值
dir1["xx"]={"schoo":"being"}
dir2 = {"value":dir1}

//遍历dir
for k,v in dir2.items():
	print k
	print v

//update:有则替换,没有则增加
tempDir={"school":"beijing"}
tempDir.update({"school":"xxx"})
print  tempDir("school") //结果为xxx

//dir取值,如果没有,则抛出异常
dir1 = {"name":"dagao","age":23}
try:
	print dir1["xx"]
except:
	print "error"

//例子
list1 = ["a","b","c"]
list2 = ["x","y","z"]

dir = {"l1":list1,"l2":list2}
print len(dir) //为2
tempList = dir["l1"]
//动态为list增加值
print tempList.append("d")
print dir

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics