Python 2次元リスト内包表記演習

python

演習

Pythonやるなら内包表記を使わないのはもったいなさすぎる!!
ということで内包表記演習です。今回は受講生の方が作ってくれた問題の中からのピックアップです。

q1

[[100, 99, 98, 97, 96, 95, 94, 93, 92, 91],
[90, 89, 88, 87, 86, 85, 84, 83, 82, 81],
[80, 79, 78, 77, 76, 75, 74, 73, 72, 71],
[70, 69, 68, 67, 66, 65, 64, 63, 62, 61],
[60, 59, 58, 57, 56, 55, 54, 53, 52, 51],
[50, 49, 48, 47, 46, 45, 44, 43, 42, 41],
[40, 39, 38, 37, 36, 35, 34, 33, 32, 31],
[30, 29, 28, 27, 26, 25, 24, 23, 22, 21],
[20, 19, 18, 17, 16, 15, 14, 13, 12, 11],
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]]

q2

[[1,0,0,0,1],
[0,1,0,1,0],
[0,0,1,0,0],
[0,1,0,1,0],
[1,0,0,0,1]]

q3

[[0, -10, -20, -30, -40, -50, -60, -70, -80, -90],
[10, 0, -10, -20, -30, -40, -50, -60, -70, -80],
[20, 10, 0, -10, -20, -30, -40, -50, -60, -70],
[30, 20, 10, 0, -10, -20, -30, -40, -50, -60],
[40, 30, 20, 10, 0, -10, -20, -30, -40, -50],
[50, 40, 30, 20, 10, 0, -10, -20, -30, -40],
[60, 50, 40, 30, 20, 10, 0, -10, -20, -30],
[70, 60, 50, 40, 30, 20, 10, 0, -10, -20],
[80, 70, 60, 50, 40, 30, 20, 10, 0, -10],
[90, 80, 70, 60, 50, 40, 30, 20, 10, 0]]

q4

[[1, 0, 0, 0, 0],
[2, 1, 0, 0, 0],
[2, 2, 1, 0, 0],
[2, 2, 2, 1, 0],
[2, 2, 2, 2, 1]]

q5

[[1, 0, 0, 0], [0, 2, 0, 0], [0, 0, 3, 0],[0, 0, 0, 4]]

q6

[[60,61,62,63,64,65,66,67,68],[70,71,72,73,74,75,76,77,78],[80,81,82,83,84,85,86,87,88],[90,91,92,93,94,95,96,97,98]]

q7

[[1, 2, 3, 4, 5, 6, 7, 8, 9],
[2, 4, 6, 8, 10, 12, 14, 16, 18],
[3, 6, 9, 12, 15, 18, 21, 24, 27],
[4, 8, 12, 16, 20, 24, 28, 32, 36],
[5, 10, 15, 20, 25, 30, 35, 40, 45],
[6, 12, 18, 24, 30, 36, 42, 48, 54],
[7, 14, 21, 28, 35, 42, 49, 56, 63],
[8, 16, 24, 32, 40, 48, 56, 64, 72],
[9, 18, 27, 36, 45, 54, 63, 72, 81]]

q8

[[7, 7, 7], [7, 3, 7], [7, 7, 7]]

q9

[[3,6,9,12,15,18,21,24,27],
[5,10,15,20,25,30,35,40,45],
[7,14,21,28,35,42,49,56,63],
[9,18,27,36,45,54,63,72,81]]

q10

[[2,4,6,8,10],
[1,3,5,7,9]]

q11

[['_', '*', '_', '*', '_', '*', '_', '*', '_', '*'],
 ['_', '_', '_', '_', '_', '_', '_', '_', '_', '_'],
 ['_', '*', '_', '*', '_', '*', '_', '*', '_', '*'],
 ['_', '_', '_', '_', '_', '_', '_', '_', '_', '_'],
 ['_', '*', '_', '*', '_', '*', '_', '*', '_', '*'],
 ['_', '_', '_', '_', '_', '_', '_', '_', '_', '_'],
 ['_', '*', '_', '*', '_', '*', '_', '*', '_', '*'],
 ['_', '_', '_', '_', '_', '_', '_', '_', '_', '_'],
 ['_', '*', '_', '*', '_', '*', '_', '*', '_', '*'],
 ['_', '_', '_', '_', '_', '_', '_', '_', '_', '_']]

q12

[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
[4, 5, 6, 7, 8, 9, 10, 11, 12, 13],
[9, 10, 11, 12, 13, 14, 15, 16, 17, 18],
[16, 17, 18, 19, 20, 21, 22, 23, 24, 25],
[25, 26, 27, 28, 29, 30, 31, 32, 33, 34],
[36, 37, 38, 39, 40, 41, 42, 43, 44, 45],
[49, 50, 51, 52, 53, 54, 55, 56, 57, 58],
[64, 65, 66, 67, 68, 69, 70, 71, 72, 73],
[81, 82, 83, 84, 85, 86, 87, 88, 89, 90]]

q13

[[10, 9, 8, 7, 6, 5, 4, 3, 2, 1],
[9, 8, 7, 6, 5, 4, 3, 2, 1],
[8, 7, 6, 5, 4, 3, 2, 1],
[7, 6, 5, 4, 3, 2, 1],
[6, 5, 4, 3, 2, 1],
[5, 4, 3, 2, 1],
[4, 3, 2, 1],
[3, 2, 1],
[2, 1],
[1]]

q1-q13解答例

import pprint

q1=[[100-(i*10+j) for j in range(10)] for i in range(10)]
pprint.pprint(q1)

q2=[[ 1 if i==j or i+j==4 else 0 for j in range(5)] for i in range(5)]
pprint.pprint(q2)

q3=[[i*10 - j*10 for j in range(10)] for i in range(10)]
pprint.pprint(q3)

q4=[[ 1 if i==j else 2 if j<i else 0 for j in range(5)] for i in range(5)]
pprint.pprint(q4)

q5=[[ j + 1 if i==j else 0 for j in range(4)] for i in range(4)]
pprint.pprint(q5)

q6=[[i*10+j for j in range(0,9)] for i in range(6,10)]
pprint.pprint(q6)

q7=[[i*j for j in range(1,10)] for i in range(1,10)]
pprint.pprint(q7)

q8=[[3 if i*j==1 else 7 for j in range(3)] for i in range(3)]
pprint.pprint(q8)

q9=[[i*j for j in range(1,10)] for i in range(3,10,2)]
pprint.pprint(q9)

q10=[[i+j*2 for j in range(5)] for i in range(2,0,-1)]
pprint.pprint(q10)

q11=[['*' if i % 2==0 and j % 2 != 0 else '_' for j in range(10)] for i in range(10)]
pprint.pprint(q11)

q12=[[ i**2 + j for j in range(10)] for i in range(10)]
pprint.pprint(q12)

q13=[[ i - j for j in range(i)] for i in range(10,0,-1)]
pprint.pprint(q13)
python
スポンサーリンク
シェアする
mjpurinをフォローする

コメント

タイトルとURLをコピーしました