Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with Generator
#1
Hi all

I am trying to learn about generators. The code below is showing some strange behaviours (the aim is to calculate the average raised Amt for each company in round "a"):

techcruncher.csv:

Output:
permalink,company,numEmps,category,city,state,fundedDate,raisedAmt,raisedCurrency,round digg,Digg,60,web,San Francisco,CA,01-Dec-06,8500000,USD,b digg,Digg,60,web,San Francisco,CA,01-Oct-05,2800000,USD,a facebook,Facebook,450,web,Palo Alto,CA,01-Sep-04,500000,USD,angel facebook,Facebook,450,web,Palo Alto,CA,01-May-05,12700000,USD,a photobucket,Photobucket,60,web,Palo Alto,CA,01-Mar-05,3000000,USD,a
file_name = "techcruncher.csv"
lines = (line for line in open(file_name))
list_line = (s.rstrip().split(",") for s in lines)
cols = next(list_line)
company_dicts = (dict(zip(cols,data)) for data in list_line)
unique_companies = (
    company_dict["company"]
    for company_dict in company_dicts
    if company_dict["round"] == ("a".lower())
)
number_of_companies = len(list(unique_companies))
print(number_of_companies)
I get the output below, which is what I expect.

Output:
PS D:\python> python generatorlearn.py 3
However, when I change the code to this:

file_name = "techcruncher.csv"
lines = (line for line in open(file_name))
list_line = (s.rstrip().split(",") for s in lines)
cols = next(list_line)
company_dicts = (dict(zip(cols,data)) for data in list_line)
funding = (
    int(company_dict["raisedAmt"])
    for company_dict in company_dicts
    if company_dict["round"] == ('A'.lower())
)
total_series_a = sum(funding)
print(total_series_a)
unique_companies = (
    company_dict["company"]
    for company_dict in company_dicts
    if company_dict["round"] == ("a".lower())
)
number_of_companies = len(list(unique_companies))
print(number_of_companies)
(new lines are 6-12 inclusive), I get the following output:

Output:
PS D:\python> python generatorlearn.py 18500000 0
Somehow the generator unique_companies do not seem to work anymore.
Would appreciate if someone could explain why is this the case.
Thanks in advance!
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 692 Nov-07-2023, 08:40 AM
Last Post: perfringo
  list call problem in generator function using iteration and recursive calls postta 1 2,013 Oct-24-2020, 09:33 PM
Last Post: bowlofred
  receive from a generator, send to a generator Skaperen 9 5,694 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