Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何从数组打印表?_Python_Arrays_List_Printing - Fatal编程技术网

Python 如何从数组打印表?

Python 如何从数组打印表?,python,arrays,list,printing,Python,Arrays,List,Printing,我有4个数组,我想打印到如下表中: Item Code, Item, Price, Item Stock 001, Pencil, 10, 738 从这4个数组依此类推: item_code = ["Item Code", "001", "002", "003", "004", "005", "006", "007", "0

我有4个
数组
,我想打印到如下表中:

Item Code, Item, Price, Item Stock
001, Pencil, 10, 738 
从这4个
数组依此类推:

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

如何做到这一点?

一种选择是使用熊猫:

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

一种选择是使用熊猫:

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
您可以使用:

z=zip(商品代码、商品、商品价格、商品库存)
对于代码、i、价格、z中的库存:
打印(代码、i、价格、库存)
您可以使用:

z=zip(商品代码、商品、商品价格、商品库存)
对于代码、i、价格、z中的库存:
打印(代码、i、价格、库存)
使用一个简单的for循环:


对于范围内的i(长度(项目代码)):
打印(物料编码[i]、物料[i]、物料价格[i]、物料库存[i])
使用一个简单的for循环:


对于范围内的i(长度(项目代码)):
打印(物料编码[i]、物料[i]、物料价格[i]、物料库存[i])

使用简单的for循环,您可以对库存商品执行相同的操作

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

使用简单的for循环,您可以对库存的物品执行相同的操作

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

使用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

使用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

你不应该使用他没有要求的库给出答案,这可以很容易地完成,没有它你不应该使用他没有要求的库给出答案,这可以很容易地完成,没有itor更好:
对于z中的i:print(*i)
或者更好:
对于z中的i:print(*i)