代碼量還可以縮減嗎?
# coding=utf-8
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
for key, value in d.items():
? ? for index in range(len(value)):
? ? ? ? print("{}的第{}次成績是{}分".format(key,index+1,value[index]))
# coding=utf-8
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
for key, value in d.items():
? ? for index in range(len(value)):
? ? ? ? print("{}的第{}次成績是{}分".format(key,index+1,value[index]))
2020-10-18
舉報(bào)
2023-10-18
# Enter a code
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
for key in d.keys():
? ? for score in d[key]:
? ? ? ? print(key, score)
? ??
2021-01-25
for i in d.items():
? ? if i[1] <= 60:
? ? ? ? print(i[0])
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
for item in d.items():
? ? for score in item[1]:
? ? ? ? print(item[0] +':' + str(score))
2021-01-25
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
for key in d:
??? print(key)
??? value =d[key]
??? print(value)
2020-11-01
d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}
print(len(d.keys()))
不用循環(huán),d.key()獲取所有元素,再len()獲取長度就行了