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

回答編集履歴

2

タプルの例を追記

2020/04/11 07:28

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -22,4 +22,28 @@
22
22
 
23
23
  print(answer)
24
24
  print(type(answer))
25
+ ```
26
+
27
+ タプルをキーにすることもできます。
28
+
29
+ ```python
30
+ import random
31
+
32
+ fammily = ['Yamada','Takahashi']
33
+ who = ['father','mather']
34
+
35
+ data = {
36
+ ('father', 'Yamada'): ["Ichiro","42"],
37
+ ('mather', 'Yamada'): ["Hanako","40"],
38
+ ('father', 'Takahashi'): ["Jiro","45"],
39
+ ('mather', 'Takahashi'): ["Yoshie","46"],
40
+ }
41
+
42
+ fammily_choice = random.choice(fammily)
43
+ who_choice = random.choice(who)
44
+
45
+ answer = data[who_choice, fammily_choice]
46
+
47
+ print(answer)
48
+ print(type(answer))
25
49
  ```

1

回答修正

2020/04/11 07:27

投稿

shiracamus
shiracamus

スコア5406

answer CHANGED
@@ -1,7 +1,25 @@
1
+ 質問内容を勘違いしてすみません。
1
- list関に文字列を渡と1文字ずつリスト変換します
2
+ 名で処理は良くないので、辞書データにして処理することをお勧めします。
2
3
 
3
4
  ```python
4
- answer = [who_choice, fammily_choice]
5
+ import random
5
- ```
6
6
 
7
+ fammily = ['Yamada','Takahashi']
8
+ who = ['father','mather']
9
+
10
+ data = {
11
+ 'father_Yamada': ["Ichiro","42"],
12
+ 'mather_Yamada': ["Hanako","40"],
13
+ 'father_Takahashi': ["Jiro","45"],
14
+ 'mather_Takahashi': ["Yoshie","46"],
15
+ }
16
+
17
+ fammily_choice = random.choice(fammily)
18
+ who_choice = random.choice(who)
19
+ choiced = who_choice + '_' + fammily_choice
20
+
21
+ answer = data[choiced]
22
+
7
- にしてはいかがですか?
23
+ print(answer)
24
+ print(type(answer))
25
+ ```