• <tfoot id='ijWLP'></tfoot>
      • <bdo id='ijWLP'></bdo><ul id='ijWLP'></ul>

      <small id='ijWLP'></small><noframes id='ijWLP'>

    1. <i id='ijWLP'><tr id='ijWLP'><dt id='ijWLP'><q id='ijWLP'><span id='ijWLP'><b id='ijWLP'><form id='ijWLP'><ins id='ijWLP'></ins><ul id='ijWLP'></ul><sub id='ijWLP'></sub></form><legend id='ijWLP'></legend><bdo id='ijWLP'><pre id='ijWLP'><center id='ijWLP'></center></pre></bdo></b><th id='ijWLP'></th></span></q></dt></tr></i><div id='ijWLP'><tfoot id='ijWLP'></tfoot><dl id='ijWLP'><fieldset id='ijWLP'></fieldset></dl></div>

        <legend id='ijWLP'><style id='ijWLP'><dir id='ijWLP'><q id='ijWLP'></q></dir></style></legend>
      1. 有很多字典的列表 VS 有很少列表的字典?

        List with many dictionaries VS dictionary with few lists?(有很多字典的列表 VS 有很少列表的字典?)
          <bdo id='lhBe5'></bdo><ul id='lhBe5'></ul>

            <small id='lhBe5'></small><noframes id='lhBe5'>

              <tfoot id='lhBe5'></tfoot>
              1. <i id='lhBe5'><tr id='lhBe5'><dt id='lhBe5'><q id='lhBe5'><span id='lhBe5'><b id='lhBe5'><form id='lhBe5'><ins id='lhBe5'></ins><ul id='lhBe5'></ul><sub id='lhBe5'></sub></form><legend id='lhBe5'></legend><bdo id='lhBe5'><pre id='lhBe5'><center id='lhBe5'></center></pre></bdo></b><th id='lhBe5'></th></span></q></dt></tr></i><div id='lhBe5'><tfoot id='lhBe5'></tfoot><dl id='lhBe5'><fieldset id='lhBe5'></fieldset></dl></div>
                    <tbody id='lhBe5'></tbody>

                  <legend id='lhBe5'><style id='lhBe5'><dir id='lhBe5'><q id='lhBe5'></q></dir></style></legend>
                1. 本文介绍了有很多字典的列表 VS 有很少列表的字典?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在用这样的数据集做一些练习:

                  I am doing some exercises with datasets like so:

                  包含许多词典的列表

                  users = [
                      {"id": 0, "name": "Ashley"},
                      {"id": 1, "name": "Ben"},
                      {"id": 2, "name": "Conrad"},
                      {"id": 3, "name": "Doug"},
                      {"id": 4, "name": "Evin"},
                      {"id": 5, "name": "Florian"},
                      {"id": 6, "name": "Gerald"}
                  ]
                  

                  列表很少的字典

                  users2 = {
                      "id": [0, 1, 2, 3, 4, 5, 6],
                      "name": ["Ashley", "Ben", "Conrad", "Doug","Evin", "Florian", "Gerald"]
                  }
                  

                  Pandas 数据框

                  import pandas as pd
                  pd_users = pd.DataFrame(users)
                  pd_users2 = pd.DataFrame(users2)
                  print pd_users == pd_users2
                  

                  问题:

                  1. 我应该像 users 还是 users2 那样构建数据集?
                  2. 是否存在性能差异?
                  3. 一个比另一个更易读吗?
                  4. 是否有我应该遵循的标准?
                  5. 我通常将这些转换为 pandas 数据帧.当我这样做时,两个版本是相同的......对吗?
                  6. 每个元素的输出都是正确的,所以我是否使用 panda df 并不重要?

                  推荐答案

                  这涉及到 面向列的数据库 与面向行.您的第一个示例是面向行的数据结构,第二个示例是面向列的.在 Python 的特定情况下,第一个可以使用 slots 显着提高效率,这样不需要为每一行复制列字典.

                  This relates to column oriented databases versus row oriented. Your first example is a row oriented data structure, and the second is column oriented. In the particular case of Python, the first could be made notably more efficient using slots, such that the dictionary of columns doesn't need to be duplicated for every row.

                  哪种形式效果更好在很大程度上取决于您如何处理数据;例如,如果您只访问任何行的所有内容,那么面向行是很自然的.同时,当您按特定字段搜索时,面向列可以更好地利用缓存等(在 Python 中,这可能会因大量使用引用而减少;类型如 array 可以优化它).传统的面向行的数据库经常使用面向列的排序索引来加快查找速度,了解这些技术后,您可以使用键值存储实现任意组合.

                  Which form works better depends a lot on what you do with the data; for instance, row oriented is natural if you only ever access all of any row. Column oriented meanwhile makes much better use of caches and such when you're searching by a particular field (in Python, this may be reduced by the heavy use of references; types like array can optimize that). Traditional row oriented databases frequently use column oriented sorted indices to speed up lookups, and knowing these techniques you can implement any combination using a key-value store.

                  Pandas 确实将您的两个示例转换为相同的格式,但是对于面向行的结构,转换本身的成本更高,因为必须读取每个单独的字典.所有这些成本都可能是微不足道的.

                  Pandas does convert both your examples to the same format, but the conversion itself is more expensive for the row oriented structure, simply because every individual dictionary must be read. All of these costs may be marginal.

                  第三个选项在您的示例中不明显:在这种情况下,您只有两列,其中一列是从 0 开始的连续范围内的整数 ID.这可以按条目本身的顺序存储,这意味着整个结构将在您调用的列表中找到 users2['name'];但值得注意的是,没有位置的条目是不完整的.该列表使用 enumerate() 转换为行.数据库通常也有这种特殊情况(例如,sqlite rowid).

                  There's a third option not evident in your example: In this case, you only have two columns, one of which is an integer ID in a contiguous range from 0. This can be stored in the order of the entries itself, meaning the entire structure would be found in the list you've called users2['name']; but notably, the entries are incomplete without their position. The list translates into rows using enumerate(). It is common for databases to have this special case also (for instance, sqlite rowid).

                  一般来说,从保持代码合理的数据结构开始,并仅在您了解自己的用例并存在可衡量的性能问题时进行优化.Pandas 之类的工具可能意味着大多数项目无需微调即可正常运行.

                  In general, start with a data structure that keeps your code sensible, and optimize only when you know your use cases and have a measurable performance issue. Tools like Pandas probably means most projects will function just fine without finetuning.

                  这篇关于有很多字典的列表 VS 有很少列表的字典?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding config modes to Plotly.Py offline - modebar(将配置模式添加到 Plotly.Py 离线 - 模式栏)
                  Plotly: How to style a plotly figure so that it doesn#39;t display gaps for missing dates?(Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙?)
                  python save plotly plot to local file and insert into html(python将绘图保存到本地文件并插入到html中)
                  Plotly: What color cycle does plotly express follow?(情节:情节表达遵循什么颜色循环?)
                  How to save plotly express plot into a html or static image file?(如何将情节表达图保存到 html 或静态图像文件中?)
                  Plotly: How to make a line plot from a pandas dataframe with a long or wide format?(Plotly:如何使用长格式或宽格式的 pandas 数据框制作线图?)

                    <bdo id='hPwt4'></bdo><ul id='hPwt4'></ul>

                          <i id='hPwt4'><tr id='hPwt4'><dt id='hPwt4'><q id='hPwt4'><span id='hPwt4'><b id='hPwt4'><form id='hPwt4'><ins id='hPwt4'></ins><ul id='hPwt4'></ul><sub id='hPwt4'></sub></form><legend id='hPwt4'></legend><bdo id='hPwt4'><pre id='hPwt4'><center id='hPwt4'></center></pre></bdo></b><th id='hPwt4'></th></span></q></dt></tr></i><div id='hPwt4'><tfoot id='hPwt4'></tfoot><dl id='hPwt4'><fieldset id='hPwt4'></fieldset></dl></div>

                          <small id='hPwt4'></small><noframes id='hPwt4'>

                          <tfoot id='hPwt4'></tfoot>
                            <tbody id='hPwt4'></tbody>

                          <legend id='hPwt4'><style id='hPwt4'><dir id='hPwt4'><q id='hPwt4'></q></dir></style></legend>