按创建的时间对globals()的项进行排序

2024-10-01 11:36:27 发布

您现在位置:Python中文网/ 问答频道 /正文

在IPython工作时,我输入了3个作业:

In [1]: foo = 1

In [2]: bar = 2

In [3]: zoo = 3

我打算把他们的口述拿来。你知道吗

In [4]: globals()
Out[4]: 
{...
 'bar': 2,
 'exit': <IPython.core.autocall.ExitAutocall at 0x10e61b048>,
 'foo': 1,
 'get_ipython': <bound method InteractiveShell.get_ipython of <IPython.terminal.interactiveshell.TerminalInteractiveShell object at 0x10e617208>>,
 'quit': <IPython.core.autocall.ExitAutocall at 0x10e61b048>,
 'zoo': 3}

globals按字母顺序打印dict,因此我不能用一个操作选择所有dict。你知道吗

如何从globals()以assignent\u创建的顺序获取dict?你知道吗


Tags: incoregetfoo顺序ipython作业bar
1条回答
网友
1楼 · 发布于 2024-10-01 11:36:27

首先,globals不跟踪创建顺序。 其次,您可以尝试删除不需要的项,然后对字典进行排序,如下所示:

remove_keys = ['exit', 'get_ipython']
g = sorted(
        dict(
            (k,v) for k,v in globals() 
            if k not in remove_keys
        )
    )

相关问题 更多 >