从 python 字典中打印列

2023-11-29

我有一本一周多提交的字典。我想以每周日历样式的列将它们打印出来。

{
  'Fri': 
  ['Commit: 04:15PM Move flex to mixin and do mobile-first queries\n', 
   'Commit: 03:52PM use padding to get the margins\n', 
   'Commit: 10:09AM Remove field_prepared_logo height\n', 
   'Commit: 03:15PM Add the final div to footer\n', 
   'Commit: 03:05PM Merge from redesign\n'], 
  'Thu': 
  ['Commit: 10:25AM Design qa fixes on /clients page\n'], 
  'Tue': ['Commit: 09:40AM remove transform and tweak span placement in hamburger\n'], 
  'Wed': ['Commit: 02:19PM Change blockquote font and width\n']}

似乎使用numpy是将其按列打印出来的方法。我还能够通过添加一些虚拟字符串并将字典转换为数组的数组来“平衡”列表。我正在努力解决的问题是如何将数组的数组转换为正确的数组numpy array.

“均匀”数组数组:

[ 
['Commit: 09:40AM remove transform and tweak span placement in hamburger\n', 'X', 'X', 'X', 'X']
['Commit: 02:19PM Change blockquote font and width\n', 'X', 'X', 'X', 'X']
['Commit: 04:15PM Move flex to mixin and do mobile-first queries\n', 'Commit: 03:52PM use padding to get the margins\n', 'Commit: 10:09AM Remove field_prepared_logo height\n', 'Commit: 03:15PM Add the final yeti to footer\n', 'Commit: 03:05PM Merge from p-redesign\n']
['Commit: 10:25AM Design qa fixes on /clients page\n', 'X', 'X', 'X', 'X']
]

我尝试过的:

nump_array = numpy.array(array_of_arrays)
print(nump_array[:,0])

总是出错IndexError: too many indices for array. I think我需要做的就是进去并将这些内部数组转换为 numpy 数组vstack他们,但我很不清楚如何处理 numpy.我还想知道我是否不应该从一开始就这么快地把字典扔掉。

这是我正在寻找的内容的缩短版本:

|  Mon  |  Tue  |  Wed  |  Thu  |  Fri  |
| 04:15 | 09:40 |  10:32| 04:12 | 11:00 |
| Move..|Do a ..|Add .. | Use ..| Change|
| 03:52 |       |       |       |       |

我认为你可以解决这个问题,无需numpy并且仅使用 stdlib 模块!

from itertools import zip_longest

d = {'Fri': ['Commit: 04:15PM Move flex to mixin and do mobile-first queries\n',
         'Commit: 03:52PM use padding to get the margins\n',
         'Commit: 10:09AM Remove field_prepared_logo height\n',
         'Commit: 03:15PM Add the final div to footer\n',
         'Commit: 03:05PM Merge from redesign\n'],
 'Thu': ['Commit: 10:25AM Design qa fixes on /clients page\n'],
 'Tue': ['Commit: 09:40AM remove transform and tweak span placement in '
         'hamburger\n'],
 'Wed': ['Commit: 02:19PM Change blockquote font and width\n']}

for row in zip_longest(d['Tue'], d['Wed'], d['Thu'], d['Fri']):
    print(row)
# ('Commit: 09:40AM remove transform and tweak span placement in hamburger\n', 'Commit: 02:19PM Change blockquote font and width\n', 'Commit: 10:25AM Design qa fixes on /clients page\n', 'Commit: 04:15PM Move flex to mixin and do mobile-first queries\n')
# (None, None, None, 'Commit: 03:52PM use padding to get the margins\n')
# (None, None, None, 'Commit: 10:09AM Remove field_prepared_logo height\n')
# (None, None, None, 'Commit: 03:15PM Add the final div to footer\n')
# (None, None, None, 'Commit: 03:05PM Merge from redesign\n')

zip_longest消除了“平衡”阵列的需要...它只是返回None没有什么可放的地方。你也可以通过fillvalue=''或类似设置默认值。

您还可以使用有序字典来避免像我一样手动指定日期的顺序。

现在您已经有了单独的行,剩下的就是漂亮打印的练习了。这textwrap module可能是你在这里的朋友。

编辑:这需要一些工作,但这里也处理了漂亮的印刷

maxwidth = (80//len(d)) - 1  # set this to whatever value you want
wrapper = textwrap.TextWrapper(width=maxwidth, subsequent_indent=' ')

wrapped_commits = {k: [wrapper.wrap(commit) for commit in v] for k, v in d.items()}
justified_commits = {k: [line.ljust(maxwidth) for commit in v for line in commit] for k, v in wrapped_commits.items()}

for l in zip_longest(justified_commits['Tue'], justified_commits['Wed'], justified_commits['Thu'], justified_commits['Fri'], fillvalue=' '*maxwidth):
    print(' '.join(l))

这是输出:

Commit: 09:40AM     Commit: 02:19PM     Commit: 10:25AM     Commit: 04:15PM    
 remove transform    Change blockquote   Design qa fixes on  Move flex to mixin
 and tweak span      font and width      /clients page       and do mobile-    
 placement in                                                first queries     
 hamburger                                                  Commit: 03:52PM use
                                                             padding to get the
                                                             margins           
                                                            Commit: 10:09AM    
                                                             Remove field_prepa
                                                             red_logo height   
                                                            Commit: 03:15PM Add
                                                             the final div to  
                                                             footer            
                                                            Commit: 03:05PM    
                                                             Merge from        
                                                             redesign          
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从 python 字典中打印列 的相关文章

  • 将 numpy 数组写入文本文件的速度

    我需要将一个非常 高 的两列数组写入文本文件 而且速度非常慢 我发现如果我将数组改造成更宽的数组 写入速度会快得多 例如 import time import numpy as np dataMat1 np random rand 1000
  • 静态数组VS。 C++11 中的动态数组

    我知道这是一个非常古老的争论 全世界已经讨论过很多次了 但我目前很难决定在特定情况下应该使用静态数组和动态数组之间的哪种方法而不是另一种方法 实际上 我不会使用 C 11 我会使用静态数组 但我现在很困惑 因为两者可能有相同的好处 第一个解
  • 当单词以“|”分隔时如何读取文件(埃因霍温)?

    在Python中 我有一个文件 其中的单词由 例如 city state zipcode 我的文件阅读器无法区分单词 另外 我希望我的文件阅读器从第 2 行而不是第 1 行开始 如何让我的文件阅读器分隔单词 import os import
  • 更改 Altair 中的构面标题位置?

    如何将方面标题 在本例中为年份 移动到每个图的上方 默认值似乎位于图表的一侧 这可以轻易改变吗 import altair as alt from vega datasets import data df data seattle weat
  • WindowsError:[错误 126] 使用 ctypes 加载操作系统时

    python代码无法在Windows 7平台上运行 def libSO lib ctypes cdll LoadLibrary ConsoleApplication2 so lib cfoo2 1 3 当我尝试运行它时 得到来自python
  • python是带有字符串的运算符行为[重复]

    这个问题在这里已经有答案了 我无法理解以下行为 我正在创建 2 个字符串 并使用 is 运算符来比较它 对于第一种情况 它的工作方式有所不同 对于第二种情况 它按预期工作 当我使用逗号或空格时 它显示是什么原因False与比较is当没有使用
  • PySide6.1 与 matplotlib 3.4 不兼容

    当我只安装PySide6时 GUI程序运行良好 但是一旦我安装了matplotlib及其依赖包 包括pyqt5 则GUI程序将无法运行并输出以下错误消息 This application failed to start because no
  • 如何自动转换十六进制代码以将其用作 Java 中的 byte[]?

    我这里有很多十六进制代码 我想将它们放入 Java 中 而不需要向每个实体附加 0x 喜欢 0102FFAB 和我必须执行以下操作 byte test 0x01 0x02 0xFF 0xAB 我有很多很长的十六进制代码 有什么办法可以自动做
  • Python 惰性迭代器

    我试图了解迭代器表达式如何以及何时被求值 以下似乎是一个懒惰的表达 g i for i in range 1000 if i 3 i 2 然而 这个在构造上失败了 g line strip for line in open xxx r if
  • 如何使用 paramiko 查看(日志)文件传输进度?

    我正在使用 Paramiko 的 SFTPClient 在主机之间传输文件 我希望我的脚本打印文件传输进度 类似于使用 scp 看到的输出 scp my file user host user host password my file 1
  • Pandas style.bar 颜色基于条件?

    如何渲染其中一列的 Pandas dfstyle bar color属性是根据某些条件计算的 Example df style bar subset before after color ff781c vmin 0 0 vmax 1 0 而
  • Python 类型安全吗?

    根据维基百科 https en wikipedia org wiki Type system Type safety and memory safety 如果一种语言不允许违反类型系统规则的操作或转换 计算机科学家就认为该语言是 类型安全的
  • 解析根元素内元素之间的 XML 文本

    我正在尝试用 Python 解析 XML 以下是 XML 结构的示例 a aaaa1 b bbbb b aaaa2 a
  • Python 声音(“铃声”)

    我想让一个 python 程序在完成任务时通过发出嘟嘟声来提醒我 目前 我使用import os然后使用命令行语音程序说 进程完成 我更愿意它是一个简单的 铃 我知道有一个函数可以用于Cocoa apps NSBeep 但我认为这与此没有太
  • Python]将两个文本文件合并为一个(逐行)[关闭]

    Closed 这个问题需要多问focused help closed questions 目前不接受答案 我是蟒蛇新手 我想做的是将文件 a 和文件 b 逐行合并到一个文件中 例如 text file a a n b n c text fi
  • 如何将回溯/sys.exc_info() 值保存在变量中?

    我想将错误名称和回溯详细信息保存到变量中 这是我的尝试 import sys try try print x except Exception ex raise NameError except Exception er print 0 s
  • 处理大文件的最快方法?

    我有多个 3 GB 制表符分隔文件 每个文件中有 2000 万行 所有行都必须独立处理 任何两行之间没有关系 我的问题是 什么会更快 逐行阅读 with open as infile for line in infile 将文件分块读入内存
  • 如何在 robobrowser-python 中发出 POST 请求

    http robobrowser readthedocs org en latest api html http robobrowser readthedocs org en latest api html 我正在尝试使用 APIbrows
  • 如何循环遍历字典列表并打印特定键的值?

    我是 Python 新手 有一个问题 我知道这是一个非常简单的问题 运行Python 3 4 我有一个需要迭代并提取特定信息的列表 以下是列表 称为部分 的示例 已截断 数千个项目 state DEAD id phwl type name
  • 如何获取所有mysql元组结果并转换为json

    我能够从表中获取单个数据 但是当我试图获取表上的所有数据时 我只得到一行 cnn execute sql rows cnn fetchall column t 0 for t in cnn description for row in ro

随机推荐