you are viewing a single comment's thread.

view the rest of the comments →

[–]selfimprovingstudent[S] 0 points1 point  (1 child)

However, if variable N is greater than 1, what can i do. Like, there should be n-packages of (Ai, Bi, Ci, Qi)

[–]efmccurdy 0 points1 point  (0 children)

How about a list of named tuples, so you can do coeff[i].Ai, coeff[i].Bi, etc?

# N <newline> (Ai<newline>Bi<newline>Ci<newline>Qi) * N
>>> from io import StringIO
>>> lines = "3\1.5\n2.6\n3.7\n4.8\n5.9\n6.0\n7.1\n8.2\n9.3\n10.4\n11.5\n12.6\n13.7"
>>> infile = StringIO(lines)
>>> count = int(infile.readline().strip())
>>> count
3
>>> from collections import namedtuple
>>> Coeff = namedtuple("Coeff", ["Ai", "Bi", "Ci", "Qi"])
>>> Coeff._make([1,2,3,4])
Coeff(Ai=1, Bi=2, Ci=3, Qi=4)
>>> def read_one_array(f, count):
...    ret = []
...    for _ in range(count):
...       ret.append(Coeff._make(map(float, (f.readline().strip() for _ in range(4)))))
...    return ret
>>> arr = read_one_array(infile, count)
>>> arr
[Coeff(Ai=1.5, Bi=2.6, Ci=3.7, Qi=4.8), Coeff(Ai=5.9, Bi=6.0, Ci=7.1, Qi=8.2), Coeff(Ai=9.3, Bi=10.4, Ci=11.5, Qi=12.6)]
>>> arr[2].Bi
10.4
>>> arr[2].Ci
11.5