質問するログイン新規登録

回答編集履歴

5

修正

2020/06/21 22:41

投稿

ForestSeo
ForestSeo

スコア2724

answer CHANGED
@@ -15,7 +15,7 @@
15
15
  ```5行目で2次元にしています。
16
16
  [内包表記](https://note.nkmk.me/python-list-comprehension/)で書いています。
17
17
  ***
18
- 例えば以下のコードは、
18
+ リスト内包表記を使うと、例えば以下のコードは、
19
19
  ```Python
20
20
  l = [1, 2, 3]
21
21
  new = []

4

修正

2020/06/21 22:41

投稿

ForestSeo
ForestSeo

スコア2724

answer CHANGED
@@ -5,17 +5,28 @@
5
5
  ]
6
6
  lst = [s.split() for s in lst]
7
7
 
8
- # 3
9
- l = [i[0] for i in lst if len(str(int(i[4][0:2]) % int(i[4][3:6]))) == 1]
8
+ l = [i[0] for i in lst if int(i[4][0:2]) % int(i[4][3:6]) < 10] # 3
9
+ d = {i[2]:i[3] for i in lst} # 4
10
+
10
11
  print(l)
11
12
  # ['Bさん']
12
-
13
- # 4
14
- d = {i[2]:i[3] for i in lst}
15
13
  print(d)
16
14
  # {'講談社': 'teratail君', '朝日': 'teratail様'}
17
- ```5行目で以下のように2次元にしています。
15
+ ```5行目で2次元にしています。
16
+ [内包表記](https://note.nkmk.me/python-list-comprehension/)で書いています。
17
+ ***
18
+ 例えば、以下のコードは、
18
19
  ```Python
20
+ l = [1, 2, 3]
21
+ new = []
22
+ for item in l:
23
+ new.append(str(item))
19
- print(lst)
24
+ print(new)
20
- # [['Aさん', '500', '講談社', 'teratail君', '60-40'], ['Bさん', '1500', '朝日', 'teratail様', '80-78']]
25
+ # ['1', '2', '3']
26
+ ```以下のように省略できます。
27
+ ```Python
28
+ l = [1, 2, 3]
29
+ new = [str(item) for item in l]
30
+ print(new)
31
+ # ['1', '2', '3']
21
32
  ```

3

修正

2020/06/21 22:40

投稿

ForestSeo
ForestSeo

スコア2724

answer CHANGED
@@ -1,4 +1,3 @@
1
- 2がよく分からないんですが
2
1
  ```Python
3
2
  lst = [
4
3
  "Aさん 500 講談社 teratail君 60-40",

2

修正

2020/06/21 21:52

投稿

ForestSeo
ForestSeo

スコア2724

answer CHANGED
@@ -1,10 +1,10 @@
1
1
  2がよく分からないんですが
2
2
  ```Python
3
3
  lst = [
4
- "Aさん", 500, "講談社", "teratail君", "60-40",
4
+ "Aさん 500 講談社 teratail君 60-40",
5
- "Bさん", 1500, "朝日", "teratail様", "80-78",
5
+ "Bさん 1500 朝日 teratail様 80-78",
6
6
  ]
7
- lst = [lst[n:n+5] for n in range(0, len(lst), 5)]
7
+ lst = [s.split() for s in lst]
8
8
 
9
9
  # 3
10
10
  l = [i[0] for i in lst if len(str(int(i[4][0:2]) % int(i[4][3:6]))) == 1]
@@ -15,4 +15,8 @@
15
15
  d = {i[2]:i[3] for i in lst}
16
16
  print(d)
17
17
  # {'講談社': 'teratail君', '朝日': 'teratail様'}
18
- ```5行目で2次元にしています
18
+ ```5行目で以下のように2次元にしています
19
+ ```Python
20
+ print(lst)
21
+ # [['Aさん', '500', '講談社', 'teratail君', '60-40'], ['Bさん', '1500', '朝日', 'teratail様', '80-78']]
22
+ ```

1

修正

2020/06/21 21:51

投稿

ForestSeo
ForestSeo

スコア2724

answer CHANGED
@@ -15,4 +15,4 @@
15
15
  d = {i[2]:i[3] for i in lst}
16
16
  print(d)
17
17
  # {'講談社': 'teratail君', '朝日': 'teratail様'}
18
- ```
18
+ ```5行目で2次元にしています・