xstdys 2019-05-23 00:50 采纳率: 0%
浏览 824
已采纳

python遍历字典键值对的问题

使用for循环打印出字典包含的国家,结果只打印了3个国家,为啥,怎么打全?
图片说明图片说明

top3river ={
    'nile':'Tanzania',
    'nile':'Burundi',
    'nile':'Rwanda',
    'nile':'Uganda',
    'nile':'Sultan',
    'nile':'Egypte',
    'Amazon River':'Peru',
    'Amazon River':'Brazil',
    'Yangtze River':'China'
    }
for river,country in top3river.items():
    print('The ' + river.title() + ' runs through '
          + country.title() + '.')
for name in top3river.keys():
    print("The river's name is " + name +'.')
for country in top3river.values():
    print(country)


  • 写回答

3条回答 默认 最新

  • qq_36815796 2019-05-23 11:56
    关注

    python字典中的key具有唯一性,你的数据字典中只有三个key值,所以后面的{key,value}会覆盖前面的值,所以字典中只有三个国家的

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?