ホームページ >バックエンド開発 >Python チュートリアル >Python リストの生成
Python リスト生成
L = [] for x in range(1, 11): L.append(x * x) print L print '\n' print [x * x for x in range(1, 11)] print '\n' print [x * (x + 1) for x in range(1, 100, 2)]
d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } tds = ['<tr><td>%s</td><td>%s</td></tr>' % (name, score) for name, score in d.iteritems()] print '<table>' print '<tr><th>Name</th><th>Score</th><tr>' print '\n'.join(tds) print '</table>' print '\n' d = { 'Adam': 95, 'Lisa': 85, 'Bart': 59 } def generate_tr(name, score): if score < 60: return '<tr><td>%s</td><td style="color:red">%s</td></tr>' % (name, score) return '<tr><td>%s</td><td>%s</td></tr>' % (name, score) tds = [generate_tr(name, score) for name, score in d.iteritems()] print '<table border="1">' print '<tr><th>Name</th><th>Score</th><tr>' print '\n'.join(tds) print '</table>'