TypeError:reversed()的参数必须是序列

Are*_*ski 4 python reverse enumerate

怎么枚举不产生序列?

----> 1 BytesInt('1B')

     12 def BytesInt(s):
     13     suffixes = ['B','KB','MB','GB','TB','PB','EB','ZB','YB']
---> 14     for power,suffix in reversed(enumerate(suffixes)):
     15         if s.endswith(suffix):
     16             return int(s.rstrip(suffix))*1024**power

TypeError: argument to reversed() must be a sequence
Run Code Online (Sandbox Code Playgroud)

Dan*_*man 6

enumerate确实不返回序列,它是一个发生器.如果输入相对较小,可以将其转换为列表:

for power, suffix in reversed(list(enumerate(suffixes))):
Run Code Online (Sandbox Code Playgroud)