我想我在標題中沒有很好地表達自己,但基本上這就是我需要做的。我有一個非常大的串列,其中包含索引 1 的漫畫名稱,索引 2 的單價??,索引 3 的銷售數量和索引 4 的支付總額。
[['1', 'Tintin', '9.95', '3', '29.85'], ['2', 'Asterix', '12.5', '3', '37.5'], ['3', 'Asterix', '12.5', '3', '37.5'], ['4', 'Asterix', '12.5', '2', '25']
我需要找到銷售單位的總和和支付的總金額。例如,這里的 Asterix 將是:
['Asterix', 12.5, 8, 100]
有任何想法嗎?
uj5u.com熱心網友回復:
已經有很好的答案,只是為了發布更大案例的替代方案,您也可以考慮使用pandas:
import pandas as pd
purchase_list = [['1', 'Tintin', '9.95', '3', '29.85'], ['2', 'Asterix', '12.5', '3', '37.5'], ['3', 'Asterix', '12.5', '3', '37.5'], ['4', 'Asterix', '12.5', '2', '25']]
purchase_list = [(int(pid), name, float(price), int(count), float(total)) for pid, name, price, count, total in purchase_list]
df = pd.DataFrame.from_records(my_list, columns = ['id', 'Name', 'price', 'count', 'total'])
所以現在資料被轉換成pandas.DataFrame.
id Name price count total
0 1 Tintin 9.95 3.0 29.85
1 2 Asterix 12.50 3.0 37.50
2 3 Asterix 12.50 3.0 37.50
3 4 Asterix 12.50 2.0 25.00
為了對行進行分組,我們可以定義要用于分組的函式,然后使用該標準進行合并:
d = {'price': 'first', 'Name': 'first', 'count': 'sum' ,'total': 'sum'}
df_grouped = df.groupby('Name').aggregate(d)
輸出:
price Name count total
Name
Asterix 12.50 Asterix 8.0 100.00
Tintin 9.95 Tintin 3.0 29.85
uj5u.com熱心網友回復:
data = [['1', 'Tintin', '9.95', '3', '29.85'], ['2', 'Asterix', '12.5', '3',
'37.5'], ['3', 'Asterix', '12.5', '3', '37.5'], ['4', 'Asterix', '12.5',
'2', '25']]
store = {}
for i in data:
if i[1] not in store:
store[i[1]] = ['',0,0,0]
store[i[1]][0] = i[1]
store[i[1]][1] = i[2]
store[i[1]][2] = float(i[3])
store[i[1]][3] = float(i[3])
print(list(store.values()))
uj5u.com熱心網友回復:
制作一個接受給定名稱和一些資料的函式,并對其執行您描述的邏輯進行迭代。只需確保在執行加法之前強制轉換為正確的數字型別。
def stats(comic_data, name):
unit_price = None
num_sold = 0
revenue = 0
for comic in comic_data:
_, cname, unit_p, num, amnt = comic
if cname == name:
if unit_price is None:
unit_price = float(unit_p)
num_sold = int(num)
revenue = float(amnt)
return [name, unit_price, num_sold, round(revenue, 2)]
stats(data, "Asterix")
>> ['Asterix', 12.5, 8, 100.0]
uj5u.com熱心網友回復:
聽起來您實際上擁有的是元組,而它們實際上是采購訂單(或者發票?)然后對它們進行建模。
from dataclasses import dataclass
# for nice translations between the string price and the int price
from decimal import Decimal
@dataclass
class PurchaseOrder:
name: str
price: int # in cents
quantity: int
total_price: int # should be price * quantity
@classmethod
def from_tuple(cls, tup):
_, name, price, quantity, total_price = tup
price = int(Decimal(price) * 100)
quantity = int(quantity)
total_price = int(Decimal(total_price) * 100)
return cls(name, price, quantity, total_price)
raw_purchase_orders = [['1', 'Tintin', '9.95', '3', '29.85'], ['2', 'Asterix', '12.5', '3', '37.5'], ['3', 'Asterix', '12.5', '3', '37.5'], ['4', 'Asterix', '12.5', '2', '25']]
# Populate purchase orders
purchase_orders = []
for raw_po in raw_purchase_orders:
try:
po = PurchaseOrder.from_tuple(raw_po)
except ValueError: # not the right format, not enough values to unpack etc
pass
except TypeError: # wrong types in wrong places
pass
else:
purchase_orders.append(po)
現在我們有了我們所看到的正確模型,我們可以根據這些欄位做一些作業。讓我們使用排序和 itertools.groupby 以合理的格式獲得它。
from itertools import groupby
from operator import attrgetter
groups = groupby(sorted(purchase_orders, key=attrgetter('name')), attrgetter('name'))
aggregate_purchase_orders = []
for groupname, group in groups:
dataset = list(group) # otherwise we can only iterate once
name = groupname
price = sum(po.price for po in dataset)//len(dataset) # average price
quantity = sum(po.quantity for po in dataset)
total_price = sum(po.total_price for po in dataset)
po = PurchaseOrder(name, price, quantity, total_price)
aggregate_purchase_orders.append(po)
print(aggregate_purchase_orders)
# see output like:
# [
# PurchaseOrder(name='Asterix', price=1250, quantity=8, total_price=10000),
# PurchaseOrder(name='Tintin', price=995, quantity=3, total_price=2985)
# ]
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/360833.html
