-1

I have the following code that gets a list of strings as a raw_input from the user:

def Lookup(Dict_list):
    print 'Please enter one alias per line. Leave blank when finished.'
    user_alias = [alias for alias in iter (raw_input, '') ]
    print user_alias
    for row in Dict_list:

        #print "row test"
        #print row
        if user_alias in row.values():
            print row


    for row in Dict_list:
        if user_alias[0] == row['_cn6ca']:
                email1 = row['_chk2m']
                print email1
                return email1
            if user_alias[1] == row['_cn6ca']:
                email2 = row['_chk2m']
                print email2
                return email2
            if user_alias[2] == row['_cn6ca']:
                email3 = row['_chk2m']
                print email3
                return email3

Lookup(Dict_list)

How can I alter the if user_alias == row['_cn6ca']: line so that it will look up and return an email for EACH of the user inputted user aliases? (These keys will stay the same)

My goal is to store these looked up emails to paste later in a .csv


Here is what is generated in the Terminal:

Please enter one alias per line. Leave blank when finished. PaulDu Jeanell Twanna JaneyD Charlot Janessa

['PaulDu', 'Jeanell', 'Twanna', 'JaneyD', 'Charlot', 'Janessa'] logout

[Process completed]

gigawatts
  • 319
  • 2
  • 5
  • 15
  • 4
    Are you expecting SO to write every piece of this for you? – Jon Clements Oct 15 '13 at 21:53
  • 1
    What did you expect `user_alias in row.values()` to do when `user_alias` is a list of strings? Are you just tossing together random code in hopes it'll do something useful? – abarnert Oct 15 '13 at 21:59
  • I was hoping to implement something like the 3 repetitive if statements at the bottom of the code, but in loop form. I've only studied programming languages for about 3 weeks, so I understand that my questions might frustrate you, but that is not my intention. – gigawatts Oct 15 '13 at 22:18

1 Answers1

1

I have stripped some details so that if the row (which is a dict type) has the user as one if its values, then we find their email id.

    Dict_list = [{'nokey':'someval'},{'_cn6ca':'Twanna', '_chk2m': "twanna@xyz"},{'_cn6ca':'Jeanell', '_chk2m': "jeanell@xyz"}]

    #print 'Please enter one alias per line. Leave blank when finished.'
    #user_alias = [alias for alias in iter (raw_input, '') ]
    user_alias = ['PaulDu', 'Jeanell', 'Twanna', 'JaneyD', 'Charlot','Janessa']
    print user_alias
    for user in user_alias:
        for row in Dict_list:    
            if user in row.values():
                print row

    for user in user_alias:
        for row in Dict_list:
            if row.has_key('_cn6ca') and row.has_key('_chk2m'):
                if user == row['_cn6ca']:
                    email = row['_chk2m']
                    print email

Output:

    ['PaulDu', 'Jeanell', 'Twanna', 'JaneyD', 'Charlot', 'Janessa']
    {'_cn6ca': 'Jeanell', '_chk2m': 'jeanell@xyz'}
    {'_cn6ca': 'Twanna', '_chk2m': 'twanna@xyz'}
    jeanell@xyz
    twanna@xyz
  • Just to be clear, the emails that are successfully looked up are store as one variable in a list named 'email', correct? – gigawatts Oct 16 '13 at 15:57
  • hmm, for that you have to change the code a bit. Initialize "email = []" before the for loop. Then within the loop, write "email.append(row['_chk2m'])". This will get all the emails that are present into a variable of the type *list* at the end of operation. – Theja Tulabandhula Oct 16 '13 at 21:08