Using a built-in function Enumerate in Python to access an element's INDEX and VALUE.
Create a list with integers
In [1]: numbers = [10,5,3,7,4]
In [2]: type(numbers)
Out[2]: list
In [3]:
Apply built-in function Enumerate to numbers
In [3]: new_numbers = enumerate(numbers)
In [4]: new_numbers
Out[4]: <enumerate at 0x233d866db80>
Convert it to list format|
In [6]: list(new_numbers)
Out[6]: [(0, 10), (1, 5), (2, 3), (3, 7), (4, 4)]
In [7]:
Going to create a simple bar chart using the value in numbers
In [1]: numbers = [10,5,3,7,4]
In [2]: for index, value in enumerate(numbers):
...: print(f'{index:>8}{value:>8} {"|" * value}')
...:
0 10 ||||||||||
1 5 |||||
2 3 |||
3 7 |||||||
4 4 ||||
In [3]:
No comments:
Post a Comment