Accessing Variables by String Name
Problem:
You desire a function that returns a predefined list based on a string input. For instance, given a string "audio," the function should output the list ['mp3', 'wav'].
Easiest Solution with a Dictionary:
A standard dictionary can fulfill this requirement effortlessly.
1 2 3 4 5 6 |
|
Using Functions:
For more advanced applications, utilizing functions might be preferred.
Assigning a Dictionary's Get Method:
1 2 3 |
|
Wrapping a Dictionary Inside a Function:
1 2 3 4 5 6 7 |
|
Creating a Custom Class for Extensibility:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
This allows you to modify file types dynamically.
1 2 3 |
|
The above is the detailed content of How Can I Access a Predefined List Based on a String Input in Python?. For more information, please follow other related articles on the PHP Chinese website!