我有一个包含以下输入的Excel文档:
第1行:产品名称-7杯软饮料
第2行:不同的变量-600ml; 1.25lt; 2.25lt; 600ml-柠檬; 2.25lt-柠檬
第3行:Vareints的价格-35; 60; 90; 35; 90
我希望输出看起来像这样
7up soft dring 600 ml 35
7up soft drink 1.25ltr 60
7up soft drink 2.25ltr 90
7up soft drink 600ml lemon 35
7up soft drink 2.25ltr lemon 90
我已经使用python达到了这一步
import os
import pandas as pd
os.chdir('/Users/apple/Desktop')
tab1 = pd.read_excel('master.xlsx')
tab1['measure'] = tab1['measure'].apply(lambda x: str(x))
tab1['measure'] = tab1['measure'].apply(lambda x: x.split(';'))
tab1['price'] = tab1['price'].apply(lambda x: str(x))
tab1['price'] = tab1['price'].apply(lambda x: x.split(';'))
test = tab1.iloc[13]
tdc = dict.fromkeys(test.index)
for i in tdc.keys():
tdc[i] = []
tls = []
for i, j in zip(test['measure'],test['price']):
tls.append([test['products'] + ' ' + i, i, int(j)])
for x in tls:
for i, j in zip(['products', 'measure', 'price'], x):
tdc[i].append(j)
我得到了这个输出
测量
列表
['600ml','1.25lt','2.25lt','600ml-柠檬','2.25lt-柠檬']
价钱
列表
[35,60,90,35,90]
产品展示
列表
['7up汽水600毫升',
'7up汽水1.25ltr',
'7up汽水2.25ltr',
'7up汽水600ml柠檬',
' 7up软饮料2.25ltr柠檬']
如何将价格清单分配到产品清单?
答案 0 :(得分:2)
由于价格列表和产品列表是同步的(第一个产品具有第一个价格,第二个产品具有第二个价格,依此类推),因此可以使用简单的for循环:
for i in range(len(product_list)):
print('{}\t{}'.format(product_list[i], price_list[i]))
如果要创建新列表:
output = []
for i in range(len(product_list)):
output.append('{} {}'.format(product_list[i], price_list[i]))
答案 1 :(得分:1)
您需要合并两个列表中的数据。一种方法是使用zip或itertools.izip。
可产生所需输出的单线纸:
print('\n'.join(['{:29}{}'.format(product, price) for product, price in zip(test['products'], test['price'])]))
示例:
>>> test = {
... 'products': [
... '7up soft dring 600 ml',
... '7up soft drink 1.25ltr',
... '7up soft drink 2.25ltr',
... '7up soft drink 600ml lemon',
... '7up soft drink 2.25ltr lemon',
... ],
... 'price': [
... 35,
... 60,
... 90,
... 35,
... 90,
... ]
... }
# Get the required textual output:
>>> print('\n'.join(['{:29}{}'.format(product, price) for product, price in zip(test['products'], test['price'])]))
7up soft dring 600 ml 35
7up soft drink 1.25ltr 60
7up soft drink 2.25ltr 90
7up soft drink 600ml lemon 35
7up soft drink 2.25ltr lemon 90
# Get a list with (product, price) tuples
>>> zip(test['products'], test['price'])
[('7up soft dring 600 ml', 35), ('7up soft drink 1.25ltr', 60), ('7up soft drink 2.25ltr', 90), ('7up soft drink 600ml lemon', 35), ('7up soft drink 2.25ltr lemon', 90)]