Thread: Variable Value

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Chicago
    Posts
    17
    Rep Power
    0

    Variable Value


    Hello Guys, my first post here. My name is Stas, i am entering the world of python programming, and find it very interesting. I am a CG-Technical Artist, and use Python in production for simulation work.
    At the moment, i am working on a project, where i connect to POSTGRESQL database and exchange some values. I am using manuals and example codes, there is one section in a code that i don't understand, and i would appreciate if anyone could briefly explain that to me, or suggest what to google to find out more.
    I am skipping some section of the code, since i understand how it works and wrote some of it, but here is one way of variable assignment :

    Code:
    	cursor.execute("SELECT id FROM textures")
    	col_names = [cn[0] for cn in cursor.description]
    so, the first line here is a command to SQL to get values from column "id", the second line gets the information from columns using "cursor.description" , but i don't understand the way it creates the variable "col_names"
    if you need more info, let me know.
    Thanks.
    -stas
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2012
    Location
    39N 104.28W
    Posts
    158
    Rep Power
    3
    Forgive me if this isn't what you're asking but I think your problem is with the poorly documented but amazingly powerful Python feature, "List Comprehension". It is a shorthand loop structure where:
    Code:
    some_list = [f(var_i) for var_i in another_list]
    returns a list equivalent to
    Code:
    some_list=[]
    for var_i in another_list:
      some_list.append(f(var_i))
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Chicago
    Posts
    17
    Rep Power
    0
    Hello rrashkin,

    Thank you for such a quick reply!
    Yes, this is exactly what i was looking for, i was not sure what this expression was called, so i could not move on with my research as far as i wanted.

    This answers my question!
    Thanks
    -stas
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,966
    Rep Power
    481
    It's a list comprehension. Try these experiments in the interpreter to get the idea:

    Code:
    print([i**2 for i in range(5)]) # squares
    
    print([c.lower() for c in 'UPPER CASE'])
    
    print(''.join([c.lower() for c in 'UPPER CASE'])) # another silly example
    print('UPPER CASE'.lower())
    
    print([i for i in range(8) if i & 1]) # odd
    
    import pprint
    pprint.pprint([(i,j,i*j)for i in range(8) for j in range(i)]) # multiplication table
    
    {a:b for (a,b) in zip('dictionary amazing'.split(),'constructor huh'.split())}
    
    {c for c in 'the quick brown constructs sets'} # silly example
    set('the quick brown constructs sets')
    Every time I time them, list comprehensions run more slowly than explicit for loops with list.append. Before list comprehensions python programmers wrote
    Code:
    LIST = map(lambda i:i**2, range(8))
    As I recall, Guido doesn't like unnamed functions.
    [code]Code tags[/code] are essential for python code and Makefiles!
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    Chicago
    Posts
    17
    Rep Power
    0
    awesome, thanks for the list of examples, i have been playing around with some combinations.

    i am yet a little too far from noticing any performance issues, however, i will keep your comment in mind for the future,

IMN logo majestic logo threadwatch logo seochat tools logo