问答详情
源自:7-6 Python dict的特点

代码量还可以缩减吗?

# 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]))


提问者:Allenlax 2020-10-18 06:49

个回答

  • 慕无忌7227368
    2023-10-18 20:02:47

    # 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)

        


  • weixin_慕沐9146932
    2021-01-25 23:17:34

    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))


  • 慕九州2200859
    2021-01-25 11:31:43

    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 17:10:51

    d = {'Alice': [50, 61, 66], 'Bob': [80, 61, 66], 'Candy': [88, 75, 90]}

    print(len(d.keys()))

    不用循环,d.key()获取所有元素,再len()获取长度就行了