gpt4 book ai didi

python - 在 Python 中将数组转换为一个

转载 作者:太空宇宙 更新时间:2023-11-04 06:48:27 24 4
gpt4 key购买 nike

我有字典:

dict = {"basic": {"last_name":["tomson", "brown"], "first_name":["paul", "max"]}}

我想写一个函数来转换它:

table = [["tomson", "paul"],
["brown", "max"]]

对于 terminaltables 模块:

from terminaltables import AsciiTable

table_data = [["last_name", "first_name"]]
table_data.append(table)

ascii_table = AsciiTable(table_data)
print "\n" + ascii_table.table + "\n"

结果:

+----------+-----------+
|last_name |first_name |
+----------+-----------+
|tomson |paul |
|brown |max |
+----------+-----------+

请帮帮我!我为我的英语感到抱歉:)

最佳答案

如果您还需要“旋转”列表:

table = [list(reversed(x)) for x in zip(*dict['basic'].values())]

关于python - 在 Python 中将数组转换为一个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31589788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com