【问题标题】:How do I print a table from an array?如何从数组中打印表格?
【发布时间】:2020-08-04 07:37:53
【问题描述】:

我有 4 个arrays 想要打印到如下所示的表格中:

Item Code, Item, Price, Item Stock
001, Pencil, 10, 738 

等等从这4个arrays:

item_code = ["Item Code", "001", "002", "003", "004", "005", "006", "007", "008", "009", "010"]

item = ["Item", "Pencil", "Pen", "Eraser", "Paper", "Notebook", "Highlighters", "Card", "Stapler", "Paperclip", "Marker"]

item_price = ["Price", "10", "5", "2", "15", "20", "23", "26", "13", "17", "21"]

item_stock = stock() # This is calculated in another subheading

我该怎么做?

【问题讨论】:

    标签: python arrays list printing


    【解决方案1】:

    您可以为此使用zip

    z = zip(item_code, item, item_price, item_stock)
    for code, i, price, stock in z:
        print(code, i, price, stock)
    

    【讨论】:

    • 甚至更好:for i in z: print(*i)
    【解决方案2】:

    一种选择是使用 Pandas:

    import pandas as pd
    df = pd.DataFrame([item_code[1:], item[1:], item_price[1:]], index=[item_code[0], item[0], item_price[0]]).T
    print(df)
    
    
      Item Code          Item Price
    0       001        Pencil    10
    1       002           Pen     5
    2       003        Eraser     2
    3       004         Paper    15
    4       005      Notebook    20
    5       006  Highlighters    23
    6       007          Card    26
    7       008       Stapler    13
    8       009     Paperclip    17
    9       010        Marker    21
    

    【讨论】:

    • 你不应该使用他没有要求的库来给出答案,没有它很容易做到这一点
    【解决方案3】:

    使用简单的 for 循环:

    
    for i in range(len(item_code)):
      print(item_code[i], item[i], item_price[i], item_stock[i])
    

    【讨论】:

      【解决方案4】:

      使用简单的 for 循环,你可以对 item_stock 做同样的事情

      item_code = ["Item Code", "001", "002", "003", "004", "005", "006", "007", "008", "009", "010"]
      
      item = ["Item", "Pencil", "Pen", "Eraser", "Paper", "Notebook", "Highlighters", "Card", "Stapler", "Paperclip", "Marker"]
      
      item_price = ["Price", "10", "5", "2", "15", "20", "23", "26", "13", "17", "21"]
      
      for i in range (len(item)):
          
          print(item_code[i],item[i],item_price[i])
      

      输出:

      Item Code Item Price
      001 Pencil 10
      002 Pen 5
      003 Eraser 2
      004 Paper 15
      005 Notebook 20
      006 Highlighters 23
      007 Card 26
      008 Stapler 13
      009 Paperclip 17
      010 Marker 21
      

      【讨论】:

        【解决方案5】:

        使用 f 字符串更优雅的打印方式

        item_code = ["Item Code", "001", "002", "003", "004", "005", "006", "007", "008", "009", "010"]
        
        item = ["Item", "Pencil", "Pen", "Eraser", "Paper", "Notebook", "Highlighters", "Card", "Stapler", "Paperclip", "Marker"]
        
        item_price = ["Price", "10", "5", "2", "15", "20", "23", "26", "13", "17", "21"]
        
        item_stock = ["Item Stock", "10", "5", "2", "15", "20", "23", "26", "13", "17", "21"]
        
        for item_code, item, item_price, item_stock in zip(item_code, item, item_price, item_stock):
            print(f'{item_code},{item},{item_price},{item_stock}')
        
        Output:
        Item Code,Item,Price,Item Stock
        001,Pencil,10,10
        002,Pen,5,5
        003,Eraser,2,2
        004,Paper,15,15
        005,Notebook,20,20
        006,Highlighters,23,23
        007,Card,26,26
        008,Stapler,13,13
        009,Paperclip,17,17
        010,Marker,21,21
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-05-26
          • 2021-12-07
          • 2020-10-19
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多