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 8860549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:17:08+00:00 2026-06-14T15:17:08+00:00

def reportCSV(t): ret = ” for ev in t: for p in t[ev]: for

  • 0
def reportCSV(t):
    ret = ''
    for ev in t:
        for p in t[ev]:
            for w in t[ev][p]:
                ret += ','.join((ev, p, w, t[ev][p][w])) + '\n'
    return ret

What is a more pythonic way to do this, e.g. using itertools or the like?

In this case I’m just writing it out to a CSV file.
t is a dict
t[ev] is a dict
t[ev][p] is a dict
t[ev][p][w] is a float

I’m not sure how I’d use itertools.product in this case.

  • 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-14T15:17:09+00:00Added an answer on June 14, 2026 at 3:17 pm

    What you have could be rewritten as:

    def reportCSV(t):
        result = []
        for t_key, t_value in t.iteritems():
            for p_key, p_value in t_value.iteritems():
                for w_key, w_value in p_value.iteritems():
                    row_data = t_key, p_key, w_key, w_value
                    result.append(','.join(row_data))
        return '\n'.join(result)
    

    This will be faster since it joins strings together instead of performing a number of concatenations, and more efficient because it uses iteritems() which removes the need to do any extra dictionary lookups or create intermediary lists.

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

Sidebar

Related Questions

I've been struggling with this for way to long - def aws_file_exists? filename begin
I have this function inside another function: def _sum(k): return sum([(-1) ** v *
I'm following a groovy tutorial and there is a code like this: def fruit
def download_if_dne(href, filename): if os.path.isfile(filename): # print 'already downloaded:', href return False else: if
def test_method [a, b, c].map {|i| yield(i) } end If I call test_method like
def self.get(server) return unless server server = server.to_s if klass = @handlers[server] obj =
Is there a way to declare static methods in cheetah? IE snippets.tmpl #def address($address,
This seems like it should be simple, but I can't seem to find a
I have a module in a file called my_mod.rb declared like this: module Reports
I have a function: def self.run_all_scheduled_reports scheduled_queries = Reports.find( :all, :joins=> 'INNER JOIN `expected_times`

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.