Python工作笔记-dictionary的遍历以及enumerate使用以及Py3中has_key的替代

程序运行截图如下:

源码如下:



if __name__ == '__main__':
	
	diction={'1' : 'test1', '2' : 'test2', '3' : 'test3', '4' : 'testA'}
	hasKey={'2' : 'NiMei'}
	print("\nThe diction keys are ", end = " ")
	print(diction.keys())
	print("The hasKey keys are ", end = " ")
	print(hasKey.keys(), end="\n\n")
	i=0
	for element in diction:
		print( "i:" + str(i) + "\t" + "element:" + element + "\t" + "diction[element]:" +diction[element])
		i += 1
		pass
		
	print("\n\n")
	
	for j, element in enumerate(diction):
		
		print("j:" + str(j) + "\t" + "element:" +element + "\t" + "diction[j]" + diction[str(j+1)])
		pass
		
	
	if '2' in hasKey:
		print("hasKey.has_key('2') is true !")
	else:
		print("hasKey.has_key('2') is false !")
		
	if '1' in hasKey:
		print("hasKey.has_key('1') is true !")
	else:
		print("hasKey.has_key('1') is false !")
		
	pass

 

©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页