回答編集履歴

2

タプルの例を追記

2020/04/11 07:28

投稿

shiracamus
shiracamus

スコア5406

test CHANGED
@@ -47,3 +47,51 @@
47
47
  print(type(answer))
48
48
 
49
49
  ```
50
+
51
+
52
+
53
+ タプルをキーにすることもできます。
54
+
55
+
56
+
57
+ ```python
58
+
59
+ import random
60
+
61
+
62
+
63
+ fammily = ['Yamada','Takahashi']
64
+
65
+ who = ['father','mather']
66
+
67
+
68
+
69
+ data = {
70
+
71
+ ('father', 'Yamada'): ["Ichiro","42"],
72
+
73
+ ('mather', 'Yamada'): ["Hanako","40"],
74
+
75
+ ('father', 'Takahashi'): ["Jiro","45"],
76
+
77
+ ('mather', 'Takahashi'): ["Yoshie","46"],
78
+
79
+ }
80
+
81
+
82
+
83
+ fammily_choice = random.choice(fammily)
84
+
85
+ who_choice = random.choice(who)
86
+
87
+
88
+
89
+ answer = data[who_choice, fammily_choice]
90
+
91
+
92
+
93
+ print(answer)
94
+
95
+ print(type(answer))
96
+
97
+ ```

1

回答修正

2020/04/11 07:27

投稿

shiracamus
shiracamus

スコア5406

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