physics-labs-collection/labs/23/processor.py

47 lines
1.2 KiB
Python
Raw Normal View History

2023-04-27 14:17:57 +03:00
import prettytable
tables = {}
active_table_id = 0
r = 3
def avg(a):
return sum(a) / len(a)
with open("sample_data.txt") as f:
for l in f:
if l.rstrip("\n\r") == '':
continue
if l[0] == "#":
sl = l[1:].split()
if len(sl) == 2:
if sl[0] == "table":
new_id = int(sl[1])
tables[new_id] = {"s": [], "C": 0.0}
active_table_id = new_id
elif sl[0] == "C":
tables[active_table_id]["C"] = float(sl[1])
else:
tables[active_table_id]['s'].append(list(map(float, l.split())))
ti = list(tables.items())
ti.sort(key = lambda x: x[0])
for i, t in ti:
print(f"Table {i}")
s = t['s']
c = t['C']
avg_n = [avg(a) for a in zip(*s)]
print("❬n❭ : " + ", ".join(list(map(lambda x: str(round(x, r)), avg_n))))
avg_Cix_U = [c * avg_n[2*a] / avg_n[2*a+1] for a in range(2)]
print("❬Cix(Ui)❭ : " + ", ".join(list(map(lambda x: str(round(x, r)), avg_Cix_U))))
avg_Cix_final = avg(avg_Cix_U)
print(f"Результат для ❬Сix❭: {avg_Cix_final}\n")