[英]Printing dictionary in a While Loop
I have been looking around to see if anyone has actually done it but couldn't find it so hoping I can get some help here. 我一直在环顾四周,看看是否有人确实做到了,但是找不到,所以希望我能在这里得到一些帮助。
newDict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}
I created this dict and I want to use a while
loop to output it this way: 我创建了这个字典,我想使用
while
循环以这种方式输出它:
Jan 31
Feb 29
Mar 31
Apr 30
May 31
Jun 30
Jul 31
Aug 30
I am able to do it with a for
loop, just curious how it can be done with a while
loop. 我能够使用
for
循环来做到这一点,只是很好奇如何使用while
循环来完成它。
You can make your dictionary an iterator
calling iteritems
(Python 2.x), or iter
on the items()
(Python 3.x) 您可以将字典
iteritems
调用iteritems
的iterator
(Python 2.x),或对items()
iter
(Python 3.x)
# Python 2.x
from __future__ import print_function
items = newDict.iteritems()
# Python 3.x
items = iter(newDict.items())
while True:
try:
item = next(items)
print(*item)
except StopIteration:
break
Note: We're importing print_function
on Python 2.x because print
would be a statement instead of a function, and hence the line print(*item)
would actually fail 注意:我们将在Python 2.x上导入
print_function
,因为print
将是语句而不是函数,因此print(*item)
行实际上将失败
Here's another option, using the .pop
method. 这是使用
.pop
方法的另一个选项。
newDict = {
'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30,
'May':31, 'Jun':30, 'Jul':31, 'Aug':30
}
t = newDict.items()
while t:
print '%s %d' % t.pop()
typical output 典型输出
Jul 31
Jun 30
Apr 30
Aug 30
Feb 29
Mar 31
May 31
Jan 31
This code doesn't modify the contents of newDict
, since in Python 2 dict.items()
creates a list of the (key, value) pairs of the dictionary. 该代码不会修改
newDict
的内容,因为在Python 2中, dict.items()
创建了字典的(键,值)对的列表。 In Python 3 it returns a dynamic View object, which doesn't have a .pop
method, so this code won't work there. 在Python 3中,它会返回一个动态View对象,该对象没有
.pop
方法,因此该代码无法在其中运行。
Bear in mind that a dict
is an unordered collection, so the output order may not be what you expect. 请记住,
dict
是无序集合,因此输出顺序可能不是您期望的。
Use following code: 使用以下代码:
key=list(newDict.keys())
i=0
while i<len(key):
print(key[i]," ",newDict[key[i]])
i+=1
You can do this with while
loop. 您可以使用
while
循环执行此操作。
newDict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}
i = 0
while i < len(newDict):
val = newDict.items()[i]
print val[0], val[1]
i += 1
This is an absurd requirement, but here is one way to do it: 这是一个荒谬的要求,但这是实现它的一种方法:
newDict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}
while newDict:
x = next(x for x in newDict)
print(x, newDict.pop(x))
CAUTION: 警告:
After the while
has finished executing, newDIct
will be empty . 在
while
执行完毕后, newDIct
将为空 。
You can use iterator
您可以使用
iterator
Python 2.7 Python 2.7
new_dict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}
a = iter(new_dict.iteritems())
default = object()
while a:
elem = next(a, default)
# This check is to know whether iterator is exhausted or not.
if elem is default:
break
print "{} {}".format(*elem)
Python 3 Python 3
new_dict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}
a = iter(new_dict.items())
default = object()
while a:
elem = next(a, default)
# This check is to know whether iterator is exhausted or not.
if elem is default:
break
print("{} {}".format(*elem))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.