[Level 2] enumrate() in Python.
#!/bin/env python aWeek = ['Sun', 'Mon','Tue','Wed', 'Thu', 'Fri', 'Sat'] ## without enumerate() print 'without enumerate()...' index = 0 for day in aWeek: print index, day index += 1 ## without enumerate() print 'without enumerate()...' for index in range(len(aWeek)): print index, aWeek[index] ## with enumerate() print 'with enumerate()...' for (index, day) in enumerate(aWeek): print index, day
without enumerate()... 0 Sun 1 Mon 2 Tue 3 Wed 4 Thu 5 Fri 6 Sat without enumerate()... 0 Sun 1 Mon 2 Tue 3 Wed 4 Thu 5 Fri 6 Sat with enumerate()... 0 Sun 1 Mon 2 Tue 3 Wed 4 Thu 5 Fri 6 SatUse enumerate() would make code easier to read.
Wish this helps.
regards,
Stanley Huang
Comments
Post a Comment