如何在字典中以原始顺序返回键 [英] How to return keys in original order in a dict

查看:34
本文介绍了如何在字典中以原始顺序返回键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读取文件,并将信息从上到下读取到字典中.与原始文件相比,我不想以错误的顺序打印出来.

I'm reading in a file and storing the info in a dict as it reads from top to bottom. I don't want to print out in a wrong order compared to the original file.

还有一个非常小的问题:我记得在if and else语句的简短形式中看到它:

Also, a very small question: I remember seeing it somewhere a short form of the if and else statement:

if a == 'a':
    a = 'b' ? a = 'c'  

您知道确切的形式吗?

Do you know the exact form?

谢谢.

推荐答案

  1. 使用 OrderedDict .
  2. li>
  3. a = 'b' if a == 'a' else 'c'
  1. Use an OrderedDict.
  2. a = 'b' if a == 'a' else 'c'

这篇关于如何在字典中以原始顺序返回键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆