0

I am newbie, just learning python and wandering how to get char that i get from for loop as variable name and not actual char

i have 3 arrays

a=[1,2,3]
b=[2,3,4]
c=[3,4,5]

and for loop

for x in "abc":
    some_function(x)

for example this is my function...

def some_function(array):
    print(sum(array))

but it doesn't work... i want x to be 'a' but not as char, but as a variable a , array, that ill give to my function and do something

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
LongToeBoy
  • 195
  • 10
  • If you have a large number of such variables, you should really consider using a dict. See the duplicate for reasons why and good suggestions. – Thierry Lathuille Apr 07 '19 at 05:36

2 Answers2

1

Instead of using a string, use all the lists in a tuple like so:

for x in (a, b, c):
    some_function(x)

Now, some_function calculates sum of each list so the output looks like:

6
9
12

If you need to persist with using strings, create a dictionary to store the mappings and use it in your loop:

d = {'a': [1,2,3], 'b': [2,3,4], 'c': [3,4,5]}

def some_function(array):
    print(sum(array))

for x in 'abc':
    some_function(d[x])

# 6
# 9
# 12
Austin
  • 25,759
  • 4
  • 25
  • 48
  • And tuples might be a tad more efficient? – TrebledJ Apr 07 '19 at 05:26
  • thank you. of course this will work but i want to do it with string way... i am reading string from document, very long, i have unique arrays for each letter and i dont want to write if statement for each letter. isnt there way to do it without actually comparing and then sending through function with preassigned variables? need simpler way – LongToeBoy Apr 07 '19 at 05:27
  • @TrebledJ, yes, tuple iteration is faster than list (though not by big margin). Construction is the overhead. I should consider refactoring it. Thanks. – Austin Apr 07 '19 at 05:36
  • @CleverBot, as suggested in a comment to question, you should use a dictionary to store the mapping. See the link. – Austin Apr 07 '19 at 05:38
  • I updated my answer to show how it can be done. – Austin Apr 07 '19 at 05:43
  • yup dict actually worked, thank you very much – LongToeBoy Apr 07 '19 at 14:09
1

If you really want to do it using the string "abc", you can use the eval function:

for x in "abc":
    some_function(eval(x))

The normal 'proper' way to do it would be to loop through (a, b, c) like others have mentioned, but the nice thing about Python is that there are all sorts of fun ways to do things.

almiki
  • 455
  • 2
  • 4
  • It's important to mention along that `eval` is dangerous and is less adviced. – Austin Apr 07 '19 at 06:14
  • Yes I agree, especially as a python newbie you shouldn't get into the habit of using it for such things. Though it is harmless in this specific case because it's clear that the only code that could possibly get evaluated is `a`, `b`, or `c`. A similar but less dangerous approach: `some_function(globals()[x])`, assuming a, b, and c are global variables. `globals()` just returns a dictionary of the global variables. Change to `locals()` if a, b, and c are local variables in a function. – almiki Apr 07 '19 at 06:41