Reputation: 1
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?
Upvotes: 0
Views: 77
Reputation: 19
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
Upvotes: 0
Reputation: 444
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
Upvotes: 0
Reputation: 1898
Use a simple for loop:
for i in range(len(item_code)):
print(item_code[i], item[i], item_price[i], item_stock[i])
Upvotes: 0
Reputation: 652
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)
Upvotes: 2
Reputation: 3739
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
Upvotes: 0