0

I want to know how can i convert (probably) the user input equiv to variable to replace it

i expect when I input slice1 it will print a1 a2 a3 but i found s l i c e 1 how can i fix it

This is code is an exemple

List = [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']]
slice1 = List[0]
slice2 = List[1]
temp = input("slice number")
for item in temp:
    print(i)`
1
  • l1 = [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']]; print(l1[int(input("Enter slice number:"))]). The slice number should be within the list index. Or else it will throw error. Commented Jan 27, 2017 at 11:39

3 Answers 3

1

You should use a dictionary.

lists = {"slice1":["a1", "a2", "a3"], "slice2":["b1", "b2", "b3"]}
userInput = input("Enter Slice Number")

varName = "slice"+userInput
print(" ".join(lists[varName]))

slice1 is what is called a key. Keys are immutable items (usually strings, but can be integers or tuples). Each key has a value, for example, slice1 has a value ["a1", "a2", "a3"]. You can access values just like you would in a list, but instead of using the index, you use the key.

So when you want to access ["a1", "a2", "a3"] you do lists["slice1"]. This has the advantage over using globals as you can easily add new keys and change values. Also, it is bad practice to change globals directly.

" ".join(lists["slice1"])

" ".join(list) gets all the items of list and puts them together using the text " " as a separator.

The code I have above ends up doing this

>>>Enter Slice Number1
a1 a2 a3
>>> 

So when you enter "1" you get "a1 a2 a3"

Sign up to request clarification or add additional context in comments.

Comments

0

I am not sure I completely understood your question.

What about this solution:

List = [['a1', 'a2', 'a3'], ['b1', 'b2', 'b3']]
slice1 = List[0]
slice2 = List[1]
user_input = input("slice number")

if user_input == 'slice1':
    for item in slice1:
        print item
elif user_input == 'slice2':
    for item in slice2:
        print item
else:
    print "Slice not found :("

Requirements were changed by the OP, here's my solution using a dictionary:

slices_dictionary = {'slice1':['a1', 'a2', 'a3'], 'slice2':['b1', 'b2', 'b3']}

user_input = input("slice number")

def check_slice(user_input):
    if user_input in slices_dictionary:
        for item in slices_dictionary[user_input]:
            print item
    else:
        print "Sorry but your slice was not found!"

check_slice(user_input)

5 Comments

this is run when i have only two lists but when i have +500 list what can i do ... i search a method to make the user input like a variable in order to not to write elif condition : traitement for each variable
Ah this was not in your requirement... Can you please elaborate or provide a more detailed example?
I think you need to stop using List and you should use a dictionary instead
Hi @a7me3D please check my updated version of your code
ahh this was long time ago (first steps with python) but thanks anyway amigos
0

You can try using globals() to access variables by name

For example:

List = [[a1, a2, a3], [b1, b2, b3]]
slice1 = List[0]
slice2 = List[1]
temp = input("slice number")
slice_list = globals()[temp] 
for item in slice_list: 
    print(item)

4 Comments

Error line6 : print(print(globals()['slice' + item])) KeyError: 's'
If you use use 'for item in temp' you'll get each token from the input separately. If you want to write 'slice1 slice2' as the input, you can split temp by spaces and run on the output (you'll also need to remove 'slice' from the key given to globals)
i want to write slice1 and i get 'a1' 'a2' 'a3'
List = [[a1, a2, a3], [b1, b2, b3]] slice1 = List[0] slice2 = List[1] temp = input("slice number") slice_list = globals()[temp] for item in slice_list: print(item)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.