m0_56312662
.cpp.py.c
2021-05-15 17:01
采纳率: 72.7%
浏览 40

想整一个列表与字典的嵌套并进行输出,大佬们帮忙看看哪里不对。

pets=['birds','dogs','fish','trees']

birds={
    'owner':'Tom',
    'legs_number':'two',
    'movement_way':'fly',
}
dogs={
    'owner':'Bob',
    'leg_number':'four',
    'movement_way':'run',
}
fish={
    'owner':'helen',
    'leg_number':'zero',
    'movement_way':'swim',
}
trees={
    'owner':'mike',
    'leg_number':'zero',
    'movement_way':'NONE',
}
for creatures in pets:
    print(f"Creature's feature:"
          f"\nCreature_name:{creatures}")
    for features in creatures:
        print(f"Owner: {features['owner']}")
        print(f"legs_number:{features['leg_number']}")
        print(f"movement_way:{features['movement_way']}")
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • technologist_32
    CSDN专家-Time 2021-05-15 17:11
    已采纳

    你少写了代码。。

    creatures是string数组,代码写的是对象数组,需要对数组进行组装

    birds={
        'owner':'Tom',
        'legs_number':'two',
        'movement_way':'fly',
    }
    dogs={
        'owner':'Bob',
        'leg_number':'four',
        'movement_way':'run',
    }
    fish={
        'owner':'helen',
        'leg_number':'zero',
        'movement_way':'swim',
    }
    trees={
        'owner':'mike',
        'leg_number':'zero',
        'movement_way':'NONE',
    }
    pets=[birds,dogs,fish,trees]
    for creatures in pets:
        print(f"Creature's feature:"
              f"\nCreature_name:{creatures}")
        for k,v in creatures.items():
            print(f'{k}:{v}')
    
    点赞 评论
  • technologist_24
    CSDN专家-黄老师 2021-05-15 17:05
    birds={
        'owner':'Tom',
        'legs_number':'two',
        'movement_way':'fly',
    }
    dogs={
        'owner':'Bob',
        'leg_number':'four',
        'movement_way':'run',
    }
    fish={
        'owner':'helen',
        'leg_number':'zero',
        'movement_way':'swim',
    }
    trees={
        'owner':'mike',
        'leg_number':'zero',
        'movement_way':'NONE',
    }
    pets=[birds,dogs,fish,trees]
    for creatures in pets:
        print(f"Creature's feature:"
              f"\nCreature_name:{creatures}")
        for k, v in creatures.items():
            print(f'{k}==={v}')
    

    点赞 评论
  • technologist_32
    CSDN专家-Time 2021-05-15 17:06

    你少写了代码。。

    点赞 评论
  • technologist_16
    birds={
        'owner':'Tom',
        'leg_number':'two',
        'movement_way':'fly',
    }
    dogs={
        'owner':'Bob',
        'leg_number':'four',
        'movement_way':'run',
    }
    fish={
        'owner':'helen',
        'leg_number':'zero',
        'movement_way':'swim',
    }
    trees={
        'owner':'mike',
        'leg_number':'zero',
        'movement_way':'NONE',
    }
    pets=[birds,dogs,fish,trees] #列表中是字典的变量,不要加引号,否则就是与字典无关的字符串了
    
    for features in pets:
        print(f"Owner: {features['owner']}")
        print(f"legs_number:{features['leg_number']}")
        print(f"movement_way:{features['movement_way']}")
    
    点赞 评论
  • QA_Assistant
    有问必答小助手 2021-05-17 14:48

    您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

    如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

    ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

    点赞 评论

相关推荐