我正在为此
class Product:
def __init__(self, date=0, product_name=0,qty=0,supplier=0):
self.product_name = product_name
self.date = date
self.qty = qty
self.supplier= supplier
self.my_list = []
def purchase(self, date, product_name, qty, supplier_name ):
self.my_list.append([supplier_name,date,product_name,qty])
def calculation(self):
for i in self.my_list:
print(i)
choice=None
p=Product()
while True:
choice=int(input("1 for the add record\n2 For the display result.\n"))
if choice == 1:
product_name=input("Enter the product name\n")
qty = int(input("Enter the qty.\n"))
date= input("Enter the date")
supplier_name = input("Enter the supplier name.\n ")
p.purchase(date,product_name,qty, supplier_name)
elif choice == 2:
p.calculation()
执行此操作后,我已经添加了这样的数据...当我们选择2号选项时,我正在获得这样的数据
eg.[supplier_name, date, product_name, quantity]
[supplierA, 2019-01-01, pencil, 20]
[supplierA, 2019-01-01, pencil, 30]
[supplierA, 2018-02-02, pen, 20]
[supplierB, 2017-02-02, scale, 10]
[supplierB, 2017-10-10, scale, 20]
[supplierC, 2019-01-01, pencil,10]
[supplierC, 2019-01-01, pencil,10]
[supplierC, 2019-01-01, pencil,10]
我希望通过某种方式过滤此数据,如果日期和产品名称相同,则应添加其数量。且必须按供应商名称分组。我的意思是
中仅添加单个供应商的日期和数量expected output is
Supplier A:
[2019-01-01, pencil, 50]
[2018-02-02, pen, 20]
Supplier B:
[2017-02-02, scale, 10]
[2017-10-10, scale, 20]
Supplier C:
[2019-01-01, pencil, 30]
我尝试使用lambda和filter,但无法成功。知道如何使之成为可能吗?
答案 0 :(得分:0)
这会起作用,我认为:
class Product:
#init
def __init__(self, date=0, product_name=0,qty=0,supplier=0):
self.product_name = product_name
self.date = date
self.qty = qty
self.supplier= supplier
#make self.my_dict
self.my_dict={}
def purchase(self, date, product_name, qty, supplier_name ):
#make a new key if needing
try:
#add the data to the list
self.my_dict[supplier_name].append([date,product_name,qty])
except:
#make a new list with data
self.my_dict[supplier_name] = [[date,product_name,qty]]
def calculation(self):
#getting keys
for i in list(self.my_dict):
#print key
print(i)
#get items
for k in self.my_dict[i]:
#print item
print(k)
choice=None
p=Product()
while True:
choice=int(input("1 for the add record\n2 For the display result.\n"))
if choice == 1:
product_name=input("Enter the product name\n")
qty = int(input("Enter the qty.\n"))
date= input("Enter the date")
supplier_name = input("Enter the supplier name.\n ")
p.purchase(date,product_name,qty, supplier_name)
elif choice == 2:
p.calculation()
答案 1 :(得分:0)
您还可以修改现有的购买方式:
handleDeleteCircuit = (event) => {
const circuitID = this.props.match.params.id;
axios.delete(`link-to-api`, this.state);
this.props.history.push('/');
this.forceUpdate();
}
答案 2 :(得分:0)
您可以这样做:
from itertools import groupby
from operator import itemgetter
def calculation(self):
# sort and group items by supplier_name, date, product_name
x = groupby(sorted(self.my_list, key=itemgetter(slice(None, 3))), itemgetter(slice(None, 3)))
# sum the quantities
y = [i + [sum(map(itemgetter(3), j))] for i, j in x]
# group items by supplier
z = [(i, list(map(itemgetter(slice(1, None)), j))) for i, j in groupby(y, itemgetter(0))]
# output
for supplier, values in z:
print("{0}:".format(supplier))
print("\n".join(map(str, values)))