enumerate
doesn't produce a sequence because it's useful to be able to enumerate any iterable, not just sequences. Eg, you can enumerate over an infinite generator.
from random import randint
def randgen(lo, hi):
while True:
yield randint(lo, hi)
for i, v in enumerate(randgen(1, 6)):
print(i, v)
if i == 20:
break
Martijn has shown a nice way to do what you want by calling reversed
on the sequence. And as Daniel points out, for such a small sequence converting the iterable produced by enumerate
to a list is quite acceptable, as well as resulting in compact code.
Another option would be to zip
a range with your reversed sequence, but I think Daniel's and Martijn's ways are better here.
def BytesInt(s):
suffixes = ['B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB']
for power, suffix in zip(range(len(suffixes) - 1, -1, -1), reversed(suffixes)):
if s.endswith(suffix):
return int(s.rstrip(suffix)) * 1024 ** power
for s in ('1B', '1KB', '1TB'):
print(BytesInt(s))
output
1
1024
1099511627776