Asked  7 Months ago    Answers:  5   Viewed   48 times

I have any string. like 'buffalo',

x='buffalo'

I want to convert this string to some variable name like,

buffalo=4 

not only this example, I want to convert any input string to some variable name. How should I do that (in python)?

 Answers

51
x='buffalo'    
exec("%s = %d" % (x,2))

After that you can check it by:

print buffalo

As an output you will see: 2

Tuesday, June 1, 2021
 
felipsmartins
answered 7 Months ago
91

If it's a global variable then window[variableName] or in your case window["onlyVideo"] should do the trick.

Tuesday, June 1, 2021
 
ManojGeek
answered 7 Months ago
52

No, this is not possible. This sort of functionality is common in scripting languages like Ruby and Python, but C++ works very differently from those. In C++ we try to do as much of the program's work as we can at compile time. Sometimes we can do things at runtime, and even then good C++ programmers will find a way to do the work as early as compile time.

If you know you're going to create a variable then create it right away:

int count;

What you might not know ahead of time is the variable's value, so you can defer that for runtime:

std::cin >> count;

If you know you're going to need a collection of variables but not precisely how many of them then create a map or a vector:

std::vector<int> counts;

Remember that the name of a variable is nothing but a name — a way for you to refer to the variable later. In C++ it is not possible nor useful to postpone assigning the name of the variable at runtime. All that would do is make your code more complicated and your program slower.

Tuesday, June 1, 2021
 
Deyson
answered 7 Months ago
22

If you need to preserve the order of the elements in the list then, you can use a the sorted function and set comprehension with map like this:

lst = [0, 1], [0, 4], [1, 0], [1, 4], [4, 0], [4, 1]
data = {tuple(item) for item in map(sorted, lst)}
# {(0, 1), (0, 4), (1, 4)}

or simply without map like this:

data = {tuple(sorted(item)) for item in lst}

Another way is to use a frozenset as shown here however note that this only work if you have distinct elements in your list. Because like set, frozenset always contains unique values. So you will end up with unique value in your sublist(lose data) which may not be what you want.

To output a list, you can always use list(map(list, result)) where result is a set of tuple only in Python-3.0 or newer.

Sunday, July 4, 2021
 
Novalirium
answered 6 Months ago
32

You have (at least) two different versions of Python installed and you're mixing their files. Make sure that $PYTHONPATH, $PYTHONHOME and sys.path only contain folders for a single Python installation. In your case, one installation is in /usr/local and the other is probably in /usr.

Also, you can try installing virtualenvwrapper and setting up separate python environment to alleviate any conflicts you might be having. Here is a tutorial for installing and using virtualenv.

Wednesday, August 4, 2021
 
Alix
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share