header = hn[0]
hn = hn[1:]
print(header)
print (hn[:5])
ask_posts = []
show_posts = []
other_posts = []
for row in hn:
title = row[1]
title = title.lower()
if title.startswith('ask hn'):
ask_posts.append(title)
elif title.startswith('show hn'):
show_posts.append(title)
else:
other_posts.append(title)
print('Number of posts that start with ask_hn:', len(ask_posts))
print('Number of posts that start with show_hn:', len(show_posts))
print('Number of other post types:', len(other_posts))
print(row)
What I expected to happen:
I visited the community a few minutes ago because I was getting the wrong output for this code.
Then I saw the solution you gave a fellow DQ learner and saw that the significant difference between my code and his was that I passed [ask_posts] into the print() function as opposed to passing it in without the brackets.
I fixed only that and expected a wrong output seeing that I used ‘title’ instead of ‘row’.
What actually happened:
Number of posts that start with ask_hn: 1744
Number of posts that start with show_hn: 1162
Number of other post types: 17194
['11680777', 'RoboBrowser: Your friendly neighborhood web scraper', 'https://github.com/jmcarp/robobrowser', '182', '58', 'pmoriarty', '5/12/2016 1:43']
Meanwhile, I still got this output when I replaced ‘title’ with ‘row’
I’ll appreciate any help understanding why this is so.
Are you absolutely sure that print(title) and print(row) print out the exact same result? And that you are running the Cells in the correct order without any additional changes?
Because I tried your code, and I get the output as expected -
For print(row)
[‘11680777’, ‘RoboBrowser: Your friendly neighborhood web scraper’, ‘https://github.com/jmcarp/robobrowser’, ‘182’, ‘58’, ‘pmoriarty’, ‘5/12/2016 1:43’]
For print(title)
robobrowser: your friendly neighborhood web scraper
Let me know if I misunderstood your question or not. But, based on your description it seems that you are asking about why the output from those two print statements is the same.
The issue I had was with appending ‘title’ instead of ‘row’(I missed this in the instructions).
My initial confusion was with how I was getting the same numerical values (number of posts) in both instances.
But then again, I was appending just the title of the post instead of the entire row.