【问题标题】:Printing multiple tables together in Python在 Python 中一起打印多个表
【发布时间】:2018-04-22 02:44:20
【问题描述】:

我有这个代码:

import numpy as np

headings = np.array(["userID","name","bookingID"])
data = np.array([[1111111,"Joe Bloggs",2222222][1212121,"Jane Doe",3333333]])

如何以这种方式打印这些数据:

userID
1111111
name
Joe Bloggs
bookingID
2222222
userID
1212121
name
Jane Doe
bookingID
3333333

这是我得到的最接近的...

keys = headings.values
datas = df.values
for i in datas:
    for j in keys:
        print(j)
        print(i)

结果:

userID
[1111111 'Joe Bloggs' 2222222]
name
[1111111 'Joe Bloggs' 2222222]
bookingID
[1111111 'Joe Bloggs' 2222222]
userID
[1212121 'Jane Doe' 3333333]
name
[1212121 'Jane Doe' 3333333]
bookingID
[1212121 'Jane Doe' 3333333]

但是我正在努力将其缩小到每个键的特定数据的额外步骤......有人可以帮忙吗? (也有人可以推荐一个更好的问题标题吗?)

【问题讨论】:

    标签: python numpy printing tabular


    【解决方案1】:
    import numpy as np
    
    headings = np.array(["userID","name","bookingID"])
    data = np.array([[1111111,"Joe Bloggs",2222222],[1212121,"Jane Doe",3333333]])
    
    
    for sublist in data: #iterate through data
        for ind, value in enumerate(sublist): #iterate through each sublist  and remember index
            print(headings[ind]) #print the element in headings that corresponds to index
            print(value) #print the element
    

    输出:

    >>>userID
    >>>1111111
    >>>name
    >>>Joe Bloggs
    >>>bookingID
    >>>2222222
    >>>userID
    >>>1212121
    >>>name
    >>>Jane Doe
    >>>bookingID
    >>>3333333
    

    【讨论】:

    • 很高兴我能帮上忙 :)
    猜你喜欢
    • 2021-04-24
    • 1970-01-01
    • 1970-01-01
    • 2017-10-09
    • 2011-08-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-24
    • 1970-01-01
    相关资源
    最近更新 更多