how can I avoid abusing lists?
Thomas Nelson
thn at mail.utexas.edu
Fri Jul 7 15:10:04 EDT 2006
Thanks to everyone who posted. First, I don't think my question was
clear enough: Rob Cowie, Ant, Simon Forman, mensanator at aol.com, and Jon
Ribbens offered solutions that don't quite work as-is, because I need
multiple values to map to a single type. Tim Chase and Bruno
Destuilliers both offer very nice OOP solutions, and I think this is
the route I will probably go. However, for the simplest and easiest
solution, I really like this from Peter Otten:
inflated = [0]*5
groups = [[0,1,3],[4],[2]]
for value in [1,1,0,4]:
inflated[value] += 1
print [sum(inflated[i] for i in group) for group in groups]
4 lines (one more to assign the lists to name values, but that's
minor), and intuitive. If I had just thought of this to begin with, I
wouldn't have bothered posting.
Thanks to all for the advice.
THN
More information about the Python-list
mailing list