Python: Can a str called from a list to be use as a variable?
use string as variable name python
python assign list values to variables
python use string as new variable name
make a variable name from a string python
python dynamic variable name
python list of variable names
python rename variable with string
I am new to coding. I am trying to create code that will substitute random words into preset patterns. I am stuck trying to insert a variable to call a list of words from which to grab a random word. Instead, Python pulls a random letter from the variable. I would like Python to call the list of words and pull a random element from the list.
I've tried creating a dict and calling the variable as a value but that produced the same result. Again, this is all new to me, so sorry if the answer is simple. There is probably a method call I don't know about or I am just on the wrong track here. Any help is appreciated. Thanks!
GH= ['Filmed','Stonehenge','Summer'] LM= ['apple','cat',"lemon"] TQ= ['bear','orange','hat'] pats_lst = [["GH","LM","TQ"],["LM","GH","TQ"]] # grab a random pattern pat = (random.choice(pats_lst)) # pull random words from lists rand_words = [random.choice(item) for item in pat] # *** pulls random letter from str # does not call list print(pat) print(rand_words) # returns #['LM', 'GH', 'TQ'] #['M', 'H', 'Q'] ** not ,for ex., ['cat','Summer','orange']
As Bazingaa told in is comment, you can simply create a list of objects, instead of a list of strings!
pats_lst = [[GH, LM, TQ], [LM, GH, TQ]]
Lists can be of any kind of objects and they don't even need to be of the same kind of objects.
Playing with Python strings, lists, and variable names — or, a , Playing with Python strings, lists, and variable names — or, a complex answer to The easiest is to use the “list” class, and apply it to a function: In such a case, the “list” class (which can be called, like a function, and is thus� However, you can also use the Python while loop to iterate over each element of a list. There are five elements in the list variable. If you use for loop, the iteration performs through each element for 5 times and print each element.
If you put the values in pats_lst array into quotes, then they will be strings, if you remove the quote marks, from its values, then they will be variables, and you will get the random words.
11. Lists — How to Think Like a Computer Scientist: Learning with , The values that make up a list are called its elements, or its items. We will use the term element or item to mean the same thing. We have already seen that we can assign list values to variables or pass lists as parameters to functions: Since strings are immutable, Python optimizes resources by making two names that� Rules for Python variables: A variable name must start with a letter or the underscore character; A variable name cannot start with a number; A variable name can only contain alpha-numeric characters and underscores (A-z, 0-9, and _ ) Variable names are case-sensitive (age, Age and AGE are three different variables)
You have to change:
pats_lst = [["GH","LM","TQ"],["LM","GH","TQ"]]
pats_lst = [[GH, LM, TQ], [LM, GH, TQ]]
In first part pats_lst, you have stored list of strings instead of storing value from your variables GH, LM, TQ. So remove the quotes from each value of pats_lst
An Introduction to Python Lists, Python also supports computed lists, called “list comprehensions”. You can also use the built-in list type object to create lists: L = list() # empty list And Python never creates a new list if you assign a list to a variable. If a list contains strings, you can combine the string into a single long string using the join string method: Each element in mixed_numbers has int() called on it to convert any str values to int values. sorted() is then called and can successfully compare each element and provide a sorted output. Python can also implicitly convert a value to another type.
Programming FAQ — Python 3.8.5 documentation, What are the rules for local and global variables in Python? Why do lambdas What does the slash(/) in the parameter list of a function mean? How do I convert a string to a number? How do How do I use strings to call functions/ methods? Appending an item to the end of a Python list. When you want your Python code to add a new item to the end of a list, use the .append() method with the value you want to add inside the parentheses. You can use either a variable name or a literal value inside the quotation marks.
Storing Multiple Values in Lists – Programming with Python, Yes, we can use negative numbers as indices in Python. Data which can be modified in place is called mutable, while data which cannot be This does not mean that variables with string or number values are constants, but when we want� __str__ is only called when a string representation is required of an object. For example str(uno), print "%s" % uno or print uno. However, there is another magic method called __repr__ this is the representation of an object. When you don't explicitly convert the object to a string, then the representation is used.
Chapter 4 – Lists, And since lists themselves can contain other lists, you can use them to arrange Then I'll briefly cover the list-like tuple and string data types and how they Say you have the list ['cat', 'bat', 'rat', 'elephant'] stored in a variable named spam . [expression for item in list] We can now identify where list comprehensions are used. If you noticed, human is a string, not a list. This is the power of list comprehension. It can identify when it receives a string or a tuple and work on it like a list. You can do that using loops. However, not every loop can be rewritten as list comprehension.
- Remove the quotes from the list names
- Hint: you need to start distinguish variable name from variable value. One may need quoting, while other definitely do not.
- Perhaps you can add some code examples showing what you mean? Code is more understandable than words ;)