Python 在While循环中打印字典

Python 在While循环中打印字典,python,python-2.7,Python,Python 2.7,我一直在四处寻找,看看是否有人真的这样做了,但找不到,所以希望我能在这里得到一些帮助 newDict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30} 我创建了这个dict,我想使用while循环以这种方式输出它: Jan 31 Feb 29 Mar 31 Apr 30 May 31 Jun 30 Jul 31 Aug 30 我能够用for循环来完成它,只是好奇如何用while

我一直在四处寻找,看看是否有人真的这样做了,但找不到,所以希望我能在这里得到一些帮助

newDict = {'Jan':31, 'Feb':29, 'Mar':31, 'Apr':30, 'May':31, 'Jun':30, 'Jul':31, 'Aug':30}
我创建了这个dict,我想使用while循环以这种方式输出它:

Jan 31
Feb 29
Mar 31
Apr 30
May 31
Jun 30
Jul 31
Aug 30
我能够用for循环来完成它,只是好奇如何用while循环来完成它

使用以下代码:

key=list(newDict.keys())
i=0
while i<len(key):
    print(key[i]," ",newDict[key[i]])
    i+=1
您可以使用while循环来实现这一点


您可以将字典设置为一个迭代器,调用iteritemspython2.x,或者在项目Python 3.x上调用iter

# 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

注意:我们在Python2.x上导入print_函数,因为print将是一个语句而不是函数,因此print*行项目实际上会失败

这是一个荒谬的要求,但有一种方法可以做到这一点:

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))
警告:

while完成执行后,newDIct将为空。

您可以使用

Python 2.7

Python 3


这里有另一个选项,使用.pop方法

典型输出

这段代码不修改newDict的内容,因为在Python 2中dict.items创建了字典的键、值对列表。在Python3中,它返回一个动态视图对象,该对象没有.pop方法,因此该代码在那里不起作用


记住,DICT是一个无序的集合,所以输出顺序可能不是你所期望的。

我很好奇:为什么要使用一个while循环?@莫伯格HI,我想尝试不同的方式来看看我如何能够打印字典而不是使用更容易使用的循环。你应该认真考虑学习Python 3,Python 2将在2020年正式结束。这会在我的“dict_itemiterator”对象上抛出一个错误,该对象没有属性“next”。你试过了吗?有dict.iteritems,它已经是一个迭代器了。但是您应该使用next函数,而不是直接调用.next方法。FWIW,在Python3中,.next不存在,它现在就存在了。uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。x@Ev.Kounis它print*item在Python 2中不起作用,因此必须使用print item[0],item[1]。这是所有方法中最有效的方法。为了让它更好,我下一步就放弃,在newDict中使用一个常规的for x,在处理newDict.popx之后使用break。这并不荒谬,因为有些情况下,字典会占用大量内存,您只需处理一条记录并将其删除,因此,您在处理过程中释放了内存data@hndcrftd迭代对象发生更改的for循环不是最佳循环。不过,您可以得到dict的长度,并对其进行迭代。这样你就不需要了break@ev-好吧,不,我不能。使用for的单个迭代的全部目的是获取一个键,因为字典没有索引,目标也不是创建任何列表。休息基本上使下一个不必要,但除此之外,我正在做你做的事。
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))
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)
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))
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()
Jul 31
Jun 30
Apr 30
Aug 30
Feb 29
Mar 31
May 31
Jan 31