I have a type from a dictionary (example)
l =('1037_97',["a","b","c","d","e"])
I wish to save a file (las format) but Liblas can write only single point.
for l in Groups.iteritems():
for p in xrange(len(l[1])):
file_out.write(l[1][p])
I am trying to use if it’s possible a List Comprehensions in order to save code and speed the loop
If you want a shorter solution, consider using
map()for inner cycle, or even for both. But it is unlikely to get a significant performance boost. However,for p in l[1]:still may be faster than that construction withxrange. The following example should do what you wanted in a single line:Now let’s compare performance of different implementations. Here I tried to measure times on some test data:
And the result is:
As you can see, your initial version is the slowest, fixing iteration helps a lot,
map()version is short, but not as fast as the version withitervalues(). List comprehension that creates unneeded lists is not bad, but still slower than the plain cycle.