Pythonメモ

http://fmhr.github.io/2015/02/14/Python-yukicoder149-151/

↑からこっちにコピペ

itertools.combinations(iter,y)

from itertools import combinations
for i in combinations(range(4),3):
    print(i)
(0, 1, 2)
(0, 1, 3)
(0, 2, 3)
(1, 2, 3)

set()

c = (1,2,3)
m =  (0,1,2,3,4,5,6,7)
if set(c) < set(m):
    print('in')
else:
    print('not in')
in

リスト内包記法

a = (3,5,1)
b = (1,1,1,1,1,1,1,1,1,1)
s = sum([b[i] for i in range(len(b)) if i in a]) 
print(s)
3

スライス

s = 'あいうえ'
print(len(s))
print(s[0:0+2])
4
あい

range()

for i in range(1,5,1):
    print(i)
1
2
3
4

リスト

a = [0, 1, 2]
print(a[-2])
print(a[-3])
print(a[-4])
1
0
Traceback (most recent call last):
  File "Untitled 7", line 4, in <module>
    print(a[-4])
IndexError: list index out of range