[Tutor] multiple assignments when reading a file
Dave Angel
davea at davea.name
Thu Jul 11 07:15:42 CEST 2013
On 07/10/2013 11:24 PM, Sivaram Neelakantan wrote:
>
> I'm aware of
>
> var1, var2 = lines.split()
>
> kind of assignments
>
> How do I get to do
>
> x1..xn = lines.split()
>
> instead of typing out n var names? Is there some pythonic way to map
> all the data fields automagically given a starter var?
>
> I have dataset that's got 100s of fields and I really don't want to
> type them out.
>
That's a pretty common question here. And the right answer is "you
don't want to, not really."
The real question is what you mean by "var names." If you mean you want
the particular function you're in to have 217 extra locals, with names
something like
field000, field001, ... field216
then you're just out of luck. The names have to be known at the
function's compile time, so somewhere you have to type them all in, in
some form of name-binding assignment. The easiest one of those is what
you already described.
field000, field001, field002 <etc.> field216 = lines.split()
There might even be a limit of how many locals a function may have, but
I don't know what it is. Probably at least 256.
Next choice? Make them all global? that indeed can be faked, by
manipulating the globals() dict. The idea is abhorrent, however, since
mutable globals are a wart on the landscape.
third choice? Put them in a list. Whaddya know, they already are. So
just assign a name to that list. Referencing them is now by subscript.
And you can actually do slices and loops readily, which you can't
easily do on "var names."
Next choice? Put them in a dict. This works just like a list, except
the subscript doesn't have to be continguous ints.
Final choice? Put them in a namespace. Something like:
class NewSpace:
pass
data = NewSpace()
for index, item in enumerate(lines.split()):
data.__dict__["x" + str(index)] = item
Actually, this doesn't get the leading zero padding, but you can enhance
it from there. To reference these, use
data.x42 and data.x0
I give the details on the last one because it's the most acceptable one,
to me, with the list being a close second.
But if this were really my problem, I'd probably be giving names to
those, not numbers, and somewhere I'd have those names all typed out, in
order. If it were in the code, it might as well be on the assignment
that gives them values. And if it comes from outside (say in the header
line of a csv file), I'd use something like:
for name, item in zip(names, lines.split()):
data.__dict__[name] = item
Now you can get at the "addr1" field by:
data.addr1
--
DaveA
More information about the Tutor
mailing list