Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 8th, 2013, 07:03 AM
pyRookie pyRookie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 pyRookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 26 sec
Reputation Power: 0
Beginner Q: for loop syntax clarification

I am a complete newbie to Python and need to work with Twitter data using Python on an immediate basis. Can anyone help me understand the last line of the following code? or any pointers to look online. I see there's a for loop but not sure how the iteration works as its different from for specification I came across in Py sofar.

Code:
import twitter # Tell Python to use the twitter package 

twitter_api = twitter.Twitter(domain="api.twitter.com",api_version='1') # create a variable named twitter_api of class "twitter.Twitter()" 

WORLD_WOE_ID = 1 # The Yahoo! Where On Earth ID for the 
entire world 

world_trends = twitter_api.trends._(WORLD_WOE_ID) # get back a callable 

[trend for trend in world_trends()[0]['trends']] 


Thanks

Reply With Quote
  #2  
Old February 8th, 2013, 08:28 AM
MrFujin's Avatar
MrFujin MrFujin is offline
Lord of the Dance
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Oct 2003
Posts: 3,129 MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level)MrFujin User rank is General 11st Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Months 2 Weeks 22 h 45 m 33 sec
Reputation Power: 1736
Take a look at this page:
https://dev.twitter.com/docs/api/1/get/trends/%3Awoeid

This is what the code connects to and retrieve data from.

Reply With Quote
  #3  
Old February 8th, 2013, 09:07 AM
pyRookie pyRookie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 pyRookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 26 sec
Reputation Power: 0
Thanks for reply!
I understand the API, but I wanted to know what kind of loop is it in the last line of code I posted in my first post with statement within square brackets []. what is it called?

Quote:
Originally Posted by MrFujin
Take a look at this page:
xx
This is what the code connects to and retrieve data from.

Reply With Quote
  #4  
Old February 8th, 2013, 11:31 AM
b49P23TIvg's Avatar
b49P23TIvg b49P23TIvg is offline
Contributing User
Dev Shed Loyal (3000 - 3499 posts)
 
Join Date: Aug 2011
Posts: 3,357 b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level)b49P23TIvg User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 3 Days 9 h 10 m 17 sec
Reputation Power: 383
example
Code:
def world_trends():
    LIST = []
    LIST.append( dict( kingsPi = ['22', '/', '7'], trends = (2, 7, 1, 8, 2, )))
    return LIST

print('output of world_trends()')
print(world_trends())

print('')

print('nasty last line of post')
print([trend for trend in world_trends()[0]['trends']])

print('')

print('simplification of nasty last line of post')
print(list(world_trends()[0]['trends']))



1) In your code
[trend for trend in world_trends()[0]['trends']]
is not stored. It would take a contrived example to make this statement have any sort of noticeable side effect.

2) Why not just use world_trends()[0]['trends'] instead? Not all iterables are lists, indeed
dict(a=3) is iterable but
dict(a=3)[0] looks up the value at key 0 not item 0.
list(dict(a=3))[0] == 'a'
__________________
[code]Code tags[/code] are essential for python code!

Reply With Quote
  #5  
Old February 8th, 2013, 11:46 AM
pyRookie pyRookie is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2013
Posts: 3 pyRookie User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 26 sec
Reputation Power: 0
Very clear explanation.. I also understand now why this code did not show any result in my pyCharm IDE output window, while it worked fine on python interpreter console.

thank you very much! :-)

Quote:
Originally Posted by b49P23TIvg
example
Code:
def world_trends():
    LIST = []
    LIST.append( dict( kingsPi = ['22', '/', '7'], trends = (2, 7, 1, 8, 2, )))
    return LIST

print('output of world_trends()')
print(world_trends())

print('')

print('nasty last line of post')
print([trend for trend in world_trends()[0]['trends']])

print('')

print('simplification of nasty last line of post')
print(list(world_trends()[0]['trends']))



1) In your code
[trend for trend in world_trends()[0]['trends']]
is not stored. It would take a contrived example to make this statement have any sort of noticeable side effect.

2) Why not just use world_trends()[0]['trends'] instead? Not all iterables are lists, indeed
dict(a=3) is iterable but
dict(a=3)[0] looks up the value at key 0 not item 0.
list(dict(a=3))[0] == 'a'

Reply With Quote
  #6  
Old February 8th, 2013, 12:32 PM
Dietrich's Avatar
Dietrich Dietrich is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2005
Posts: 482 Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level)Dietrich User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 3 Days 22 h 23 m 21 sec
Reputation Power: 63
Trying to run your code, all I get is ...
AttributeError: 'module' object has no attribute 'Twitter'

I think you get a "JavaScript Object Notation" object. Looks like a Python dictionary.
__________________
Real Programmers always confuse Christmas and Halloween because Oct31 == Dec25

Last edited by Dietrich : February 8th, 2013 at 01:04 PM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Beginner Q: for loop syntax clarification

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap