Chinaunix
标题:
while
[打印本页]
作者:
tomer
时间:
2014-11-01 11:03
标题:
while
fridge={"egg":"5","bread":"6","tomato":"6","juice":"3","apple":"5"}
fridge_list=[]
while i in fridge.keys()
fridge_list.append(i)
print(fridge_list)
复制代码
想得到fridge_list=["egg","bread","tomato","juice","apple"]
作者:
TasteOracle
时间:
2014-11-01 11:15
1、while后面需要加“:”
2、while里的变量要做初始赋值
3、while不需要,你直接打印fridge.keys()不就是你需要的吗
作者:
tomer
时间:
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'])
>>>
作者:
tomer
时间:
2014-11-01 14:04
回复
2#
TasteOracle
这里使用:
fridge={"egg":"5","bread":"6","tomato":"6","juice":"3","apple":"5"}
fridge_list=list(fridge)
print(fridge_list)
复制代码
可以得到想要的结果,但是我就想用while循环,怎么才能出这样的结果呢
作者:
whitelotus19
时间:
2014-11-01 20:14
>>> fridge={"egg":"5","bread":"6","tomato":"6","juice":"3","apple":"5"}
>>> fridge
{'tomato': '6', 'juice': '3', 'egg': '5', 'apple': '5', 'bread': '6'}
>>> [x for x in fridge.keys()]
['tomato', 'juice', 'egg', 'apple', 'bread']
>>> fridge.keys()
dict_keys(['tomato', 'juice', 'egg', 'apple', 'bread'])
>>>
复制代码
作者:
tomer
时间:
2014-11-01 21:53
回复
5#
whitelotus19
如何使用while:
作者:
whitelotus19
时间:
2014-11-03 18:01
tomer 发表于 2014-11-01 21:53
回复 5# whitelotus19
我很好奇为什么这个问题一定要用while来做?for循环不是挺简单的吗
作者:
tomer
时间:
2014-11-04 09:53
回复
7#
whitelotus19
书上要求用while
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2