How Do I Change My Timezone?

Sorry for confusing title. This program should take the letter in a string and add the word ‘List’ so that it can find the 2 list groups I created. Python is taking every character in the word ‘hList’ instead of every value in the variable hList. Is there any other way to do this?

The output I’m expecting is: 2 5 3 6 6 2

userInput = 'hi'
hList = [2, 5, 3]
iList = [6, 6, 2]
userInputLen = len(userInput)
for i in range (0, userInputLen):
    for objects in (userInput[i] + 'List'):
        print(objects)

2 ANSWERS

16 febrero, 2017 a las 10:13 am admin

see also: https://bdhacker.wordpress.com/2010/02/27/python-tutorial-dictionaries-key-value-pair-maps-basics/

userInput = 'hi'
lists = {}
lists['h'] = [2, 5, 3]
lists['i'] = [6, 6, 2]
for i in userInput:
    for objects in (lists[userInput[i]]):
        print(objects)
16 febrero, 2017 a las 10:13 am admin

Use eval :

userInput = 'hi'
hList = [2, 5, 3]
iList = [6, 6, 2]
userInputLen = len(userInput)
for i in range (0, userInputLen):
    for objects in eval(userInput[i] + 'List'):
        print(objects)

Without eval,userInput[i] + ‘List’ is a string;with eval,it points to a variable called hList or iList.

Viendo 2 respuestas - de la 1 a la 2 (de un total de 2)

You must be logged in to reply to this topic.