Skip to content

Instantly share code, notes, and snippets.

@st98
Last active August 29, 2015 14:00
Show Gist options
  • Save st98/befafe90ce39a27cf0f9 to your computer and use it in GitHub Desktop.
Save st98/befafe90ce39a27cf0f9 to your computer and use it in GitHub Desktop.
'ABCD' から [(0, 'A'), (1, 'B'), (2, 'C'), (3, 'D')] を作りたい。
s = 'ABCD'
# 1
i = 0
result = []
while True:
if i >= len(s):
break
result.append((i, s[i]))
i += 1
# 2
result = []
for x in range(len(s)):
result.append((x, s[x]))
# 3
[(x, s[x]) for x in range(len(s))]
# 4
import itertools
list(zip(itertools.count(), s))
# 5, 忘れてた
list(enumerate(s))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment