Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with Generator
#4
(Feb-13-2020, 02:37 AM)palladium Wrote: Hopefully there's no redundant stuff here
Given that you want to learn about generators, I will keep your approach although there are better ways to do this.

1. you can combine line 2 and 3 into one generator expression.
2. it's better to use with context manager to open the file
3. line 5 can be list directly, instead of making it generator expression and immediately make it list on the next line
4. same for lines 7-11

you may want to look at cav.DictReader module
and you can iterate over the file handler directly, instead of creating a generator

import csv
with open("techcruncher.csv") as f:
    rdr = csv.DictReader(f)
    round_a_funding = [int(item["raisedAmt"]) for item in rdr if item["round"] == 'a']

print(f'Average Round A funding is {sum(round_a_funding)/len(round_a_funding):.2f}')
as an alternative you may look at Pandas to process the data as dataframe.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Problem with Generator - by palladium - Feb-11-2020, 03:46 AM
RE: Problem with Generator - by buran - Feb-11-2020, 04:48 AM
RE: Problem with Generator - by palladium - Feb-13-2020, 02:37 AM
RE: Problem with Generator - by buran - Feb-13-2020, 09:16 AM
RE: Problem with Generator - by DeaD_EyE - Feb-13-2020, 01:26 PM
RE: Problem with Generator - by palladium - Feb-16-2020, 02:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  problem in using generator akbarza 2 600 Nov-07-2023, 08:40 AM
Last Post: perfringo
  list call problem in generator function using iteration and recursive calls postta 1 1,966 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  receive from a generator, send to a generator Skaperen 9 5,587 Feb-05-2018, 06:26 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020