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

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0

    Unhappy Struggling with comprehension


    Hi all,

    I am trying to iterate through a list and take only the 1st 2 items and create a new list from that... Something I thought would be simple is really making me mad and driving me to drink!

    Anyways... I am querying an LDAP directory and returning an LDAP object with the values of an attribute from a specific object.

    I can get the nth value to doing:

    Code:
    test =  permElements.get(n).encode('utf-8').split("#")
    test will then equal:
    Code:
    ['Company-Admin:First National Bank BG:8550:*', 'Company-Admin:First National Bank BG:3860:*', '8550-3860']
    Which is a list of stings...

    If I type test[0] in the python console I get:
    Code:
    'Company-Admin:First National Bank BG:8550:*'
    Which is what I expected...

    Now... If I write the comprehension:
    Code:
    print [[x[0],x[1]] for i in list(range(0,permElements.size() - 1)) for x in permElements.get(i).encode('utf-8').split("#")]
    I get:

    Code:
    [['C', 'o'], ['C', 'o'], ['E', 'a'], ['C', 'o'], ['C', 'o'], ['E', 'a'], ['C', 'o'], ['C', 'o'], ['W', 'e'], ['C', 'o'], ['C', 'o'], ['W', 'e'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o'], ['C', 'o'], ['B', 'u'], ['C', 'o']...
    What am I doing wrong??

    Am I explaining my issue well enough??
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    163
    Rep Power
    19
    What you're doing is this:
    Code:
    for i in list(range(0,permElements.size() - 1)):
      for x in permElements.get(i).encode('utf-8').split("#"):
        print (x[0], x[1])
    What you need is this:
    Code:
    for i in list(range(0,permElements.size() - 1)):
      x = permElements.get(i).encode('utf-8').split("#")
      print (x[0], x[1])
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    2
    Rep Power
    0
    Thanks noobie!

    I see the errors of my way now...

    Appreciate the prompt response and help


    Originally Posted by noobie1000
    What you're doing is this:
    Code:
    for i in list(range(0,permElements.size() - 1)):
      for x in permElements.get(i).encode('utf-8').split("#"):
        print (x[0], x[1])
    What you need is this:
    Code:
    for i in list(range(0,permElements.size() - 1)):
      x = permElements.get(i).encode('utf-8').split("#")
      print (x[0], x[1])

IMN logo majestic logo threadwatch logo seochat tools logo