Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9270155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:18:02+00:00 2026-06-18T15:18:02+00:00

I have a type from a dictionary (example) l =(‘1037_97’,[a,b,c,d,e]) I wish to save

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T15:18:03+00:00Added an answer on June 18, 2026 at 3:18 pm

    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 with xrange. The following example should do what you wanted in a single line:

    map(lambda g: map(file_out.write, g), groups.itervalues())
    

    Now let’s compare performance of different implementations. Here I tried to measure times on some test data:

    import timeit
    
    groups = dict(('1037_%d' % i, ["a","b","c","d","e"]) for i in xrange(100))
    
    class FOut(object):
        def write(self, v):
            #print v
            pass
    
    file_out = FOut()
    
    def using_map():
        map(lambda g: map(file_out.write, g), groups.itervalues())
    
    def initial_version():
        for l in groups.iteritems():
            for p in xrange(len(l[1])):
                file_out.write(l[1][p])
    
    def seq_iteration():
        for l in groups.iteritems():
            for p in l[1]:
                file_out.write(p)
    
    def seq_iteration_values():
        for l in groups.itervalues():
            for p in l:
                file_out.write(p)
    
    def list_compr():
        [[file_out.write(v) for v in g] for g in groups.itervalues()]
    
    
    
    tests = ('initial_version', 'using_map', 'seq_iteration', 'list_compr', 'seq_iteration_values')
    
    
    for test in tests:
        print test, timeit.timeit('%s()'%test, 'from __main__ import %s'%test, number=10000)
    

    And the result is:

    initial_version 0.862531900406
    using_map 0.703296899796
    seq_iteration 0.541372060776
    list_compr 0.632550954819
    seq_iteration_values 0.493131160736
    

    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 with itervalues(). List comprehension that creates unneeded lists is not bad, but still slower than the plain cycle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an object of type list from which I wish to use to
i have this simple type from an external webservice: <xsd:element name=card_number maxOccurs=1 minOccurs=1> <xsd:simpleType>
Which Activator.CreateInstance overload function to call? I have a type returned from Type proxyType
I have a Tabular type Form based upon a SELECT * FROM table type
I have an oracle table with a column from type SYS.XMLTYPE and a storage
I have queue from struct type struct test { int numbers; }; queue<test> q;
I have an anonymous type object that I receive as a dynamic from a
In my database I have changed the type of a field from VARCHAR to
I have an input of type text , from which I have already entered
I have used this query before: SELECT * FROM stone_list WHERE type IN ('ALEXANDRITE','AMETHYST')

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.