- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我从一个像这样的简单 python 字典开始:-
In [29]: date_dict
Out[29]:
{'2003-06-24': 2,
'2003-08-13': 1,
'2003-08-19': 2,
'2003-08-22': 1,
'2003-08-24': 5}
键是日期,值是整数。
我的目标是将这个字典中的数据重组为:-
{'datetime': ['2003-08-13',
'2003-08-19',
'2003-06-24',
'2003-08-24',
'2003-08-22'],
'observations': [1, 2, 2, 5, 1]}
维护datetime
键持有的列表和observations
键持有的列表之间的数据关系。
这是我完成此任务的解决方案:-
In [35]: new_dict
Out[35]: {'datetime': [], 'observations': []}
In [36]: for key, value in date_dict.iteritems():
....: new_dict['datetime'].append(key)
....: new_dict['observations'].append(value)
In [37]: new_dict
Out[37]:
{'datetime': ['2003-08-13',
'2003-08-19',
'2003-06-24',
'2003-08-24',
'2003-08-22'],
'observations': [1, 2, 2, 5, 1]}
我的问题是 - 是否有替代方法(更好,如果更有效)执行此操作?
(请注意,保持两个列表之间的数据关系至关重要,即在原始 date_dict 中,“2003-08-24”对应值“5”。数据重组后,第 3 个索引“日期时间”列表是“2003-08-24”,它与“观察列表”的第三个索引正确对应为“5”。)
最佳答案
也许:
new_dict = {'datetime': date_dict.keys(), 'observations': date_dict.values()}
如果您需要订购,请尝试使用 OrderedDict而不是听写:
Return an instance of a dict subclass, supporting the usual dict methods. An OrderedDict is a dict that remembers the order that keys were first inserted. If a new entry overwrites an existing entry, the original insertion position is left unchanged. Deleting an entry and reinserting it will move it to the end.
关于python - 重组字典键值对的更好方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13372146/
目录 一、状态变化 1.1 状态变化是什么 1.2 mutableStateListOf 和 mutableStateMapOf 二、重组的
这是我的真实数据列表的示例列表: df setNames(nm) } fun3 Unit: microseconds #> expr min lq me
在 clojure 中,我可以像这样解构 map : (let [{:keys [key1 key2]} {:key1 1 :key2 2}] ...) 这类似于 CoffeeScript 的方法
这个问题在这里已经有了答案: Re-Structuring a JSON (2 个答案) 关闭 9 年前。 我需要使用 Javascript/Jquery 将我的 JSON 重新组织成新的结构,但我
我有一个具有以下结构的表,列是出发地、目的地、旅行时间和出发时间。主键是出发地、目的地和出发时间的组合。有没有什么方法可以重新构造它,以便行中没有太多的起点和目的地重复? 最佳答案 “出发地和目的地太
需要将 JSON 重组为 Python 中的新结构。 例如: { 'a' : 1, 'b' : 1, 'd' : {'d1' : '1', 'd2' : 2}, 'm' : [
我正在努力了解重组的工作原理。虽然我理解它的基本概念,但我在使用 withProps 函数时遇到了问题。如果我尝试使用它来装饰带有附加 Prop 的组件的每个子组件,它根本无法工作。我所做的是: co
假设我有一个数据框列表。每个数据框都有这样的列: lists$a company, x, y ,z lists$b company, x, y, z lists$c company, x, y, z
好吧,我有这个数组 $city : [2] => Array ( [0] => Array ( [0] => fr
工作中的 subversion 存储库是在没有对其结构进行太多规划的情况下建立的。目前没有明确的标签、主干或分支配置,尽管通过使用 subclipse:tags 存在一些标签元数据 目前存储库的格式为
我有一个具有以下文件夹结构的 Git 存储库: allprojectfiles --otherfolders --source ----projectname ------projectname --
我有一个像这样的 git repo 结构- main-repo -file1 -file2 我想把它转换成类似的东西- main-repo -javascript -fil
我有一个像这样的 git repo 结构- main-repo -file1 -file2 我想把它转换成类似的东西- main-repo -javascript -fil
有人建议我从类结构(定义我自己的类)转移到 pandas DataFrame 领域,因为我设想对我的数据进行许多操作。 此时我有一个如下所示的数据框: ID Name Recordin
我想重构我的 pandas 数据框,其中 h1、h2 等是与小时相关的值。目前看起来像: h1 h2 h3 h4 h5 h6 h7 h8 h9
我在 postgresql 上使用查询返回这样的结果。 schedule | day | subject | grade | ========================
我有一个这样组织的数据框... **Name** | **Mealtime** | **Food** John | 8:00 am | cereal John | 1:00 pm | salad
我有基本的脚本知识,但我不知道如何解决这个问题。我正在尝试将银行自动生成的 CSV 文件转换为 YNAB(您需要预算)可以理解的格式。 YNAB 格式(所需的 csv 文件格式) Date,Payee
我正在重组我的 meteor 应用程序,突然每个逻辑(JS 脚本)与初始文件分离的 View (html 模板)都停止工作。 最初我的项目看起来像这样 -project -.meteor
我试图让 children 不影响导航栏,并像下拉菜单一样显示。 当前示例位于:dev4you.byethost15.com 函数应该如下: 用户将鼠标悬停在父项上 子项显示在类似列表、Underne
我是一名优秀的程序员,十分优秀!