当前位置: 移动技术网 > IT编程>脚本编程>Python > python字典与嵌套代码实例

python字典与嵌套代码实例

2018年03月23日  | 移动技术网IT编程  | 我要评论

创业加盟网站大全,白莉灵子,萌发菌

python字典与嵌套代码实例

#char 6
alien_0={'color':'green','points':5}
#采用键与值相互关联,且一一对应,访问可以直接访问字典中的键即可
print(alien_0['color'])
print(alien_0['points'])
#6.2使用字典
alien_0={'color':'green','points':5}
#可用变量名等于某字典里该键的值,表示对该键的索引
new_points=alien_0['points']
print('you just earn new '+str(new_points)+' points')
#添加键与值
#通过字典['键']=值进行键-值的添加
alien_0['x_points']=0
alien_0['y_points']=25
print(alien_0)
#类似得,可以通过这样建立空字典对字典进行赋值
alien_0={}
alien_0['color']='green'
print(alien_0)
#字典的值可修改,通过对键所对应的值进行赋值从而修改
#通过修改速度让某移动点的坐标发生变化
alien_0={'color':'green'}
alien_0['color']='yellow'
print(alien_0)
alien_0={'x_position':0,'y_position':25,'speed':'medium'}
print("original x_position is "+str(alien_0['x_position']))
# alien_0['speed']='fast'
if alien_0['speed']=='slow':
	x_increment=1
elif alien_0['speed']=='medium':
	x_increment=2
else:
	x_increment=3
alien_0['x_position']=alien_0['x_position']+x_increment
print('new x-position is '+str(alien_0['x_position']))
#6.2.5 删除键-值,采用del 与变量赋值类似,但删除的键值会永远消失
print(alien_0)
del alien_0['speed']
print(alien_0)
#类似对象组成字典
favourite_languages={
	'Jen':'python',
	'Sarah':'c',
	'Edward':'ruby',
	'Phil':'python'
}
#擦次用循环输出对应的键与值的对应
for name,languages in favourite_languages.items():
	print(name+" 's favourite_languages is "+languages)
#6.3遍历字典
user_0={
	'username':'efermi',
	'first':'enrico',
	'last':'fermi'
}
#采用Items方式遍历字典中对的键与值
for key,value in user_0.items():
	print("\n key "+key)
	print("value: "+value)
#遍历字典中的键
friends=['Phil','Sarah']
for name in favourite_languages.keys():
	print(name.title())
	if name in friends:
		print(name+"'s favourite language is "+favourite_languages[name])
#按顺序遍历字典中的键:
for name in sorted(favourite_languages.keys()):
	print(name+'thank you for taking the poll')
#遍历字典所有的值
for language in favourite_languages.values():
	print(language)
#会出现重复值,若要删除重复值,则采用set函数
for language in set(favourite_languages.values()):
	print(language)
#6.4嵌套
#将字典嵌套至列表中
alien_0={'color':'green','points':5}
alien_1={'color':'yellow','points':10}
alien_2={'color':'red','points':15}
aliens=[alien_0,alien_1,alien_2]
for alien in aliens:
	print(alien)

aliens=[]
for alien_number in range(30):
	new_alien={'color':'green','points':5,'speed':'slow'}
	aliens.append(new_alien)
print(aliens[0:5])
print(str(len(aliens)))

for alien in aliens[0:3]:
	if alien['color']=='green':
		alien['color']='yellow'
		alien['speed']='medium'
		alien['points']=10
	elif alien['color']=='yellow':
		alien['color']='red'
		alien['speed']='fast'
		alien['points']=15
print(aliens[0:7])

#字典嵌套列表
pizza={
	'crust':'thick',
	'toppings':['mushroom','extar cheese'],
}
print('You ordered a '+pizza['crust']+'-crust pizza '+'with the following toppings')
for topping in pizza['toppings']:
	print('\t '+topping)
#先循环字典里面的items,再循环字典中value里面的值
favourite_languages={
	'Jen':['python','ruby'],
	'Sarah':['c'],
	'Edward':['ruby','go'],
	'Phil':['python','haskell'],
}

for name,languages in favourite_languages.items():
	print('\n'+name.title()+" 's favourite languages are : ")
	for language in languages:
		print('\t'+language.title())
#字典嵌套字典
users={
	'aeinstein':{
	'first':'albert',
	'last':'einstein',
	'location':'princeton'
	},
	'mcurie':{
	'first':'marie',
	'last':'curie',
	'location':'paris'
	}
}

for username,user_info in users.items():
	print('\n username'+username)
	full_name=user_info['first']+" "+user_info['last']
	location=user_info['location']

	print('\t full_name :'+full_name.title())
	print('\t location:'+location.title())

课后题:

 

char 6 homework
#6-1
someone={'fitst_name':'fat','last_name':'teng','age':22,'city':'guangdong'}
print(someone)
#字典输出不按照输入的顺序
#6-2
favourate_numbers={'Jack':13,'Sarah':24,'Tom':28,'Jtrry':46}
for name,number in favourate_numbers.items():
	print(name+" 's favourate number is "+ str(number))
#6-3 +6-4可采用如上方法打印对应的字典值
dictionary={
	'print':'输出',
	'if':'如果',
	'else':'其他',
	'True':'真',
	'False':'假'
}
for key,value in dictionary.items():
	print(key+","+value)
#6-5
rivers={'nile':'egypr','changjiang':'china','huanghe':'china'}
for river,country in rivers.items():
	print('The '+river+" runs throuth "+ country)
for river in rivers.keys():
	print(river.title())
for country in rivers.values():
	print(country.title())
#6-6
favourite_languages={
	'Jen':'python',
	'Sarah':'c',
	'Edward':'ruby',
	'Phil':'python'
}
invaited=['Jen','Sarah']
for key in favourite_languages.keys():
	if key in invaited:
		print(key.title()+" , thank's for taking invaiting")
	if key not in invaited:
		print(key.title()+", could you please take the research")
#6-7
someone={'fitst_name':'fat','last_name':'teng','age':22,'city':'guangdong'}
anyone={'fitst_name':'wu','last_name':'yifan','age':28,'city':'gz'}
somebody={'fitst_name':'peng','last_name':'yuyan','age':30,'city':'tw'}
people=[somebody,someone,anyone]
for person in people:
	print (person)
#6-8
Sarah={'type':'dog','master':'Tim'}
Tom={'type':'cat','master':'Jerry'}
xiangxiang={'type':'pig','master':'teng'}
pets=[Sarah,Tom,xiangxiang]
for pet in pets:
	print(pet)
#6-9
favourate_places={'Tom':['NY','JP'],
'Jack':['HK','TY'],
'SR':['SH','PK'],}
for name,value in favourate_places.items():
	print(name+"'s favourate places are as following")
	for palce in value:
		print(palce)

#6-10 思路同上
#6-11 城市
cities={'SZ':{'country':'China','population':100,'fact':'rich'},
		'NY':{'country':'US','population':50,'fact':'finance'},
		'LD':{'country':'UK','population':40,'fact':'wonderful'}}
for city ,values in cities.items():
	print('the name of city is '+city+' and the info are as following')
	for key, value in values.items():
		print(key+" "+str(value))

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网