質問編集履歴
1
一部自己解決により、問題を2つから1つへ変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
python3
|
1
|
+
python3 辞書の中からランダムに取り出す方法
|
body
CHANGED
@@ -1,85 +1,12 @@
|
|
1
|
-
|
1
|
+
辞書の中から、ランダムに取り出す方法
|
2
2
|
|
3
|
-
|
4
3
|
```python
|
5
|
-
|
6
|
-
# a-z list []
|
7
|
-
import sys
|
8
|
-
sys.modules[__name__].__dict__.update(dict([(chr(x), []) for x in range(ord('a'), ord('z') + 1)]))
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
# ユーザー
|
13
|
-
# user = {'le':'1','Money':'50'}
|
14
|
-
user=[1,50]
|
15
4
|
# アイテム
|
16
5
|
item={'peach': '4', 'melon': '4', 'Cherry': '1', 'orange': '3', 'Grape': '4', 'persimmon': '3', 'strawberry': '3', 'watermelon': '7', 'pear': '4', 'grapefruit': '3', 'banana': '2', 'Apple': '5', 'pineapple': '6'}
|
17
|
-
# 取引場所
|
18
|
-
Trading_place=[]
|
19
|
-
# MAP
|
20
|
-
[[r.append([0,0]) for i in range(5)] for i2 in range(5)]
|
21
6
|
|
22
7
|
|
23
|
-
#print(len(r)),print(r)
|
24
|
-
#print(item['Apple'],user)
|
25
|
-
|
26
|
-
|
27
|
-
def Sample00(xyz):
|
28
|
-
#xyz=input().split()
|
29
|
-
xyz=['Apple','2','1']
|
30
|
-
#表示
|
31
|
-
[print([key,item[key]],'',end='') for key in item]
|
32
|
-
|
33
|
-
for key in item:
|
34
|
-
#入力した単語がkeyに引っかかるなら
|
35
|
-
if str(xyz[0])==key:
|
36
|
-
#user[1]がkeyに対する値以上なら
|
37
|
-
if int(user[1])>=int(item[key]):
|
38
|
-
#keyに対する数値をuser[1]から引く
|
39
|
-
user[1]=int(user[1])-int(item[key])
|
40
|
-
#入力した場所にkeyを入れる
|
41
|
-
r[int(xyz[1])][1]=str(key)
|
42
|
-
#入力した隣に個数を入れる
|
43
|
-
r[int(xyz[1])][0]+=1
|
44
|
-
else:
|
45
|
-
pass
|
46
|
-
#表示
|
47
|
-
print("\n"),print(r),print(),print(user)
|
48
|
-
|
49
|
-
|
50
|
-
#テスト値
|
51
|
-
#Sample00(input().split())
|
52
|
-
Sample00('Apple' '2' '1')
|
53
|
-
Sample00('Apple' '2' '1')
|
54
|
-
|
55
|
-
# ココで間違い
|
56
|
-
Sample00('banana' '7' '1')
|
57
|
-
|
58
|
-
|
59
|
-
|
60
8
|
# 辞書の中から、ランダムに取り出したい処理
|
61
9
|
import random
|
62
10
|
#print(random.choice(item.items()))
|
63
11
|
|
64
|
-
```
|
65
|
-
|
66
|
-
#実行結果
|
67
|
-
```python
|
68
|
-
['pear', '4'] ['melon', '4'] ['Grape', '4'] ['Cherry', '1'] ['banana', '2'] ['grapefruit', '3'] ['orange', '3'] ['pineapple', '6'] ['peach', '4'] ['watermelon', '7'] ['Apple', '5'] ['strawberry', '3'] ['persimmon', '3']
|
69
|
-
|
70
|
-
[[0, 0], [0, 0], [1, 'Apple'], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
|
71
|
-
|
72
|
-
[1, 45]
|
73
|
-
['pear', '4'] ['melon', '4'] ['Grape', '4'] ['Cherry', '1'] ['banana', '2'] ['grapefruit', '3'] ['orange', '3'] ['pineapple', '6'] ['peach', '4'] ['watermelon', '7'] ['Apple', '5'] ['strawberry', '3'] ['persimmon', '3']
|
74
|
-
|
75
|
-
[[0, 0], [0, 0], [2, 'Apple'], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
|
76
|
-
|
77
|
-
[1, 40]
|
78
|
-
['pear', '4'] ['melon', '4'] ['Grape', '4'] ['Cherry', '1'] ['banana', '2'] ['grapefruit', '3'] ['orange', '3'] ['pineapple', '6'] ['peach', '4'] ['watermelon', '7'] ['Apple', '5'] ['strawberry', '3'] ['persimmon', '3']
|
79
|
-
|
80
|
-
#ココでbananaと入植したのにAppleの扱いとなってしまう。
|
81
|
-
[[0, 0], [0, 0], [3, 'Apple'], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0], [0, 0]]
|
82
|
-
|
83
|
-
[1, 35]
|
84
|
-
|
85
12
|
```
|