- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python – Convert Suffix denomination to Values
When it is required to convert the suffix denomination to values, the dictionary is iterated over and the ‘replace’ method is used to convert them to values.
Example
Below is a demonstration of the same
my_list = ["5Cr", "7M", "9B", "12L", "20Tr", "30K"] print("The list is :") print(my_list) value_dict = {"M": 1000000, "B": 1000000000, "Cr": 10000000, "L": 100000, "K": 1000, "Tr": 1000000000000} my_result = [] for element in my_list: for key in value_dict: if key in element: val = float(element.replace(key, "")) * value_dict[key] my_result.append(val) print("The resultant dictionary values :") print(my_result)
Output
The list is : ['5Cr', '7M', '9B', '12L', '20Tr', '30K'] The resultant dictionary values : [50000000.0, 7000000.0, 9000000000.0, 1200000.0, 20000000000000.0, 30000.0]
Explanation
A list is defined and is displayed on the console.
Another dictionary is defined with certain denomination values.
An empty list is created.
The original list is iterated over, and the keys in the dictionary are iterated over.
If the key is present in the list, it is converted to float type, and multiplied with key of the dictionary.
This is assigned to a variable.
This variable is appended to the empty list.
This is the result which is displayed on the console.
Advertisements