1

Can I use a loop in Python to generate 10 different variables instead of computing the value of each variable separately? I can imagine doing this in C/C++ wherein I could use an index value to iterate in a loop and generate value.

v1=Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result1).netloc.encode('utf-8'))

v2=Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result2).netloc.encode('utf-8'))

v3=Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result3).netloc.encode('utf-8'))

v4=Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result4).netloc.encode('utf-8'))

v5=Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result5).netloc.encode('utf-8'))

v6=Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result6).netloc.encode('utf-8'))

v7=Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result7).netloc.encode('utf-8'))

v8=Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result8).netloc.encode('utf-8'))

v9=Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result9).netloc.encode('utf-8'))

v10=Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result10).netloc.encode('utf-8'))
0

4 Answers 4

3

The more pythonic way to do it would be to either do it as a list:

vals = []
for search_result in search_results: 
    vals.append(Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result).netloc.encode('utf-8')))
# Access via vals[0], vals[1], etc.

or as a dictionary:

vals = {}
for search_result in search_results: 
    vals[search_result] = Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result).netloc.encode('utf-8'))
# Access via vals[search_result1], vals[search_result2], etc.

If you HAD to be bad about it, you could do:

for i in xrange(10):
    search_result = locals()['search_result' + str(i)]
    locals()['v' + str(i)] = Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result).netloc.encode('utf-8'))
# Accessed via v0, v1, v2, etc.

But I would recommend against it because it is non-pythonic and more obtuse than the above solutions.

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

1 Comment

Aside from obtuseness, it's not even guaranteed to work.
1

This would be easy if you represented your values as collections. Code structure mirrors data structure; when your data is distributed over unrelated variables, your code is too.

vs = [Levenshtein.jaro_winkler(exhibitor_name, urlparse(search_result).netloc.encode('utf-8'))
      for search_result in search_results]

1 Comment

@user3450198 if you want to get the variables from namespace you could run search_results = [locals()[key] for key in locals() if key.startswith("search_result") and not key.startswith("search_results")]
0

You can use a list like so:

lst = []
for search_result in search_results:
    lst.append(
      Levenshtein.jaro_winkler(
          exhibitor_name,
          urlparse(search_results).netloc.encode('utf-8')))

This assumes that you also used a list, search_results instead of all of the indivdual search_resultN variables.

2 Comments

How can I use the above code to generate the variables v1,v2,v3...v10 ?
@user3450198 See Tim Brown's answer
0

You can use locals() to get a dict that contains the variable names as keys and their contents as values. In this way, you can access the search_result* variables programmatically, instead of typing their names out.

You could then push a new entry into locals() for the v* variable names, but why pollute the local namespace with them? They are all related to each other, so placing them into a dictionary with keys v1 through v10 keeps them together in a useful way.

apply_Levenshtein = lambda x: (  
    Levenshtein.jaro_winkler(exhibitor_name,urlparse(x).netloc.encode('utf-8'))
) 

variables = {
    "v{}".format(i):apply_Levenshtein(locals()["search_result{}".format(i)]) 
    for i in range(1, 11)
}

Now variables["v1"] will be

Levenshtein.jaro_winkler(exhibitor_name,urlparse(search_result1).netloc.encode('utf-8'))`. 

Better still would be to ensure that the search_result* variables exist in a dict or a list from the beginning, so that you don't need to use locals() at all.

1 Comment

Note that Python isn't under any obligation to pay attention to what you do to what locals() returns, as the docs warn. (PS: vars is an occasionally handy builtin, and so an unfortunate variable name..)

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.