免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
论坛 程序设计 Python while
最近访问板块 发新帖
查看: 1969 | 回复: 7
打印 上一主题 下一主题

while [复制链接]

论坛徽章:
3
天秤座
日期:2014-10-29 11:37:572015元宵节徽章
日期:2015-03-06 15:50:39NBA常规赛纪念章
日期:2015-05-04 22:32:03
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2014-11-01 11:03 |只看该作者 |倒序浏览

  1. fridge={"egg":"5","bread":"6","tomato":"6","juice":"3","apple":"5"}
  2. fridge_list=[]
  3. while i in fridge.keys()
  4. fridge_list.append(i)
  5. print(fridge_list)
复制代码
想得到fridge_list=["egg","bread","tomato","juice","apple"]

论坛徽章:
1
2015年辞旧岁徽章
日期:2015-03-03 16:54:15
2 [报告]
发表于 2014-11-01 11:15 |只看该作者
1、while后面需要加“:”
2、while里的变量要做初始赋值
3、while不需要,你直接打印fridge.keys()不就是你需要的吗

论坛徽章:
3
天秤座
日期:2014-10-29 11:37:572015元宵节徽章
日期:2015-03-06 15:50:39NBA常规赛纪念章
日期:2015-05-04 22:32:03
3 [报告]
发表于 2014-11-01 11:50 |只看该作者
回复 2# TasteOracle


    fridge.keys()结果是
dict_keys(['apple', 'tomato', 'juice', 'bread', 'egg'])
怎么还有个“dict_keys",不想要
>>> fridge={"egg":"5","bread":"6","tomato":"6","juice":"3","apple":"5"}
>>> fridge.keys()
dict_keys(['apple', 'tomato', 'juice', 'bread', 'egg'])
>>> aa=[]
>>> aa=fridge.keys()
>>> print(aa)
dict_keys(['apple', 'tomato', 'juice', 'bread', 'egg'])
>>>

论坛徽章:
3
天秤座
日期:2014-10-29 11:37:572015元宵节徽章
日期:2015-03-06 15:50:39NBA常规赛纪念章
日期:2015-05-04 22:32:03
4 [报告]
发表于 2014-11-01 14:04 |只看该作者
回复 2# TasteOracle


    这里使用:


  1. fridge={"egg":"5","bread":"6","tomato":"6","juice":"3","apple":"5"}
  2. fridge_list=list(fridge)
  3. print(fridge_list)

复制代码
可以得到想要的结果,但是我就想用while循环,怎么才能出这样的结果呢

论坛徽章:
0
5 [报告]
发表于 2014-11-01 20:14 |只看该作者
  1. >>> fridge={"egg":"5","bread":"6","tomato":"6","juice":"3","apple":"5"}
  2. >>> fridge
  3. {'tomato': '6', 'juice': '3', 'egg': '5', 'apple': '5', 'bread': '6'}
  4. >>> [x for x in fridge.keys()]
  5. ['tomato', 'juice', 'egg', 'apple', 'bread']
  6. >>> fridge.keys()
  7. dict_keys(['tomato', 'juice', 'egg', 'apple', 'bread'])
  8. >>>
复制代码

论坛徽章:
3
天秤座
日期:2014-10-29 11:37:572015元宵节徽章
日期:2015-03-06 15:50:39NBA常规赛纪念章
日期:2015-05-04 22:32:03
6 [报告]
发表于 2014-11-01 21:53 |只看该作者
回复 5# whitelotus19


    如何使用while:

论坛徽章:
0
7 [报告]
发表于 2014-11-03 18:01 |只看该作者
tomer 发表于 2014-11-01 21:53
回复 5# whitelotus19


我很好奇为什么这个问题一定要用while来做?for循环不是挺简单的吗

论坛徽章:
3
天秤座
日期:2014-10-29 11:37:572015元宵节徽章
日期:2015-03-06 15:50:39NBA常规赛纪念章
日期:2015-05-04 22:32:03
8 [报告]
发表于 2014-11-04 09:53 |只看该作者
回复 7# whitelotus19

书上要求用while
   
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP