程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python 字典 while循環

編輯:Python
# 創建一個簡單的字典
alien_0 = {
'color':'green','points':3}
# 字典訪問
print(alien_0['color'])
print(alien_0['points'])
new_point = alien_0['points']
print("you just earned " + str( new_point) + ' points.')
#添加鍵-對值
print(alien_0)
alien_0['xPosition'] = 0
alien_0['yPosition'] = 5
print(alien_0)

green
3
you just earned 3 points.
{‘color’: ‘green’, ‘points’: 3}
{‘color’: ‘green’, ‘points’: 3, ‘xPosition’: 0, ‘yPosition’: 5}

# 創建一個空字典
alien_1 = {
}
alien_1['color'] = 'yellow'
alien_1['points'] = 10
print(alien_1)
# 修改字典中的值
alien_1['color'] = 'red'
print("The alien is now " + alien_1['color'] + ".")

{‘color’: ‘yellow’, ‘points’: 10}
The alien is now red.

alien_0 = {
'x_position':0,'y_position':5,'speed':'fast'}
print("Original position: " + str(alien_0['x_position']))
# 外星人向右移動
# 根據外星人的速度決定移動距離
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: " + str(alien_0['x_position']))

Original position: 0
New x_position: 3

# 刪除鍵-對值
alien_0 = {
'color':'blue','points':10}
print(alien_0)
del alien_0['color']
print(alien_0)

{‘color’: ‘blue’, ‘points’: 10}
{‘points’: 10}

# 由類似對象組成的字典(如編程語言)
favorite_languages = {

'jen':'python',
'sarah':'c',
'phil':'ruby',
'edward':'java',
}
friends = ['phil','sarah']
for name in favorite_languages:
print(name.title())
if name in friends:
print(" hi "+name.title()+
", I see your favorite language is "+
favorite_languages[name].title()
)
if 'erin' not in favorite_languages:
print("Erin,plase take our pool")

Jen
Sarah
hi Sarah, I see your favorite language is C
Phil
hi Phil, I see your favorite language is Ruby
Edward
Erin,plase take our pool

#### 字典列表
# 生成一個空列表
alien_0 = []
# 生成100個外星人
for alien_number in range(100):
new_alien = {
'color':'red','points':10,'speed':'medium'}
alien_0.append(new_alien)
for alien in alien_0[:5]:
print(alien)
print("......")
# 顯示一共生成了多少個外星人
print("Total number of aliens: "+ str(len(alien_0)))

{‘color’: ‘red’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘red’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘red’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘red’, ‘points’: 10, ‘speed’: ‘medium’}
{‘color’: ‘red’, ‘points’: 10, ‘speed’: ‘medium’}

Total number of aliens: 100

###字典中嵌套列表
pizza = {

'crust':'thick',
'topping':['mushrooms','extra cheese'],
}
print("you ordered a "+
pizza['crust'] + "-crust pizza"+
" with the following toppings:")
for topping in pizza['topping']:
print("\t"+topping)

you ordered a thick-crust pizza with the following toppings:
mushrooms
extra cheese

### 字典中嵌套字典
users = {

'aeinstein':{

'first':'albert',
'last':'einstein',
'location':'princeton',
},
'mcurie':{

'first':'marie',
'last':'curie',
'location':'paris',
},
}
for user_name,user_info in users.items():
print("\nUserame: " + user_name)
full_name = user_info['first'] + user_info['last']
location = user_info['location']
print("\tFull name: " + full_name.title())
print("\tlocation: "+ location.title())

Userame: aeinstein
Full name: Alberteinstein
location: Princeton

Userame: mcurie
Full name: Mariecurie
location: Paris

# 列表之間的移動
unconfirmed_users = ['alice','brian','candace']
confirmed_users = []
while unconfirmed_users:
current_user = unconfirmed_users.pop()
print("Verifying user: " + current_user.title())
confirmed_users.append(current_user)
print("\nThe following users have been confirmed: ")
for confirmed_user in confirmed_users:
print(confirmed_user.title())

Verifying user: Candace
Verifying user: Brian
Verifying user: Alice

The following users have been confirmed:
Candace
Brian
Alice

# 刪除指定的所有元素
pets = ['dog','cat','fish','cat','bird','cat','dog']
while 'cat' in pets:
pets.remove('cat')
print(pets)

[‘dog’, ‘fish’, ‘bird’, ‘dog’]

# 使用用戶輸入填充字典
responses = {
}
active = True
while active:
name = input("\nWhat is your name? ")
response = input("Which mountain would you like to climb someday? ")
responses[name] = response
repeat = input("Would you like to let annother person response? (yes/no) ")
if repeat == 'no':
active = False
print("\n-----Poll Result-----")
for name,response in responses.items():
print(name + "would like to climb "+ response + " .")

What is your name? Tom
Which mountain would you like to climb someday? Huashan
Would you like to let annother person response? (yes/no)yes

What is your name? Cindy
Which mountain would you like to climb someday? Taishan
Would you like to let annother person response? (yes/no)yes

What is your name? John
Which mountain would you like to climb someday? Xiangshan
Would you like to let annother person response? (yes/no)no

-----Poll Result-----
Tomwould like to climb Huashan .
Cindywould like to climb Taishan .
Johnwould like to climb Xiangshan .


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved