简体   繁体   中英

How do I print a table from an array?

I have 4 arrays that I want to print into a table that looks like this:

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

and so on from these 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

How do I do that?

You can use zip for that:

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

One option is to use 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

Use a simple for loop:


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

Use the simple for loop, you can do the same for the 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])

output:

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

More Elegant way to print using f strings

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM