代码之家  ›  专栏  ›  技术社区  ›  j panton

在Python中同时打印多个表

  •  1
  • j panton  · 技术社区  · 6 年前

    我有以下代码:

    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]
    

    但我正在努力将其缩小到每个键的特定数据。。。有人能帮忙吗?(还有人能推荐一个更好的问题标题吗?)

    1 回复  |  直到 6 年前
        1
  •  2
  •   Primusa    6 年前
    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