0

## #Python 骰子問題

``````import random

numbers  = [random.randint(1,6) for i in range(100)]
result_dict = {}
print (numbers)
for num in numbers:
count = numbers.count(num)
result_dict[num] = count

h = max(result_dict.values())
for item in result_dict.items():
value =item[1]
if value == h:
key = item[0]
print('1~6哪一個點數出現最多次:')
print(key)
print('共出現幾次? :')
print(value)

s = min(result_dict.values())
for item in result_dict.items():
value =item[1]
if value == s:
key = item[0]
print('1~6哪一個點數出現最少次:')
print(key)
print('共出現幾次? :')
print(value)
``````

1: 出現 16次
2: 出現 28次
.
.
.
6: 出現 17次

### 5 個回答

0
feb99
iT邦見習生 ‧ 2021-05-24 17:32:57

import random

numbers = [random.randint(1,6) for i in range(100)]
result_dict = {}
#print (numbers)

for num in numbers:
count = numbers.count(num)
result_dict[num] = count

h = max(result_dict.values())
for item in result_dict.items():
value =item[1]
if value == h:
key = item[0]
print('1~6哪一個點數出現最多次:')
print(key)
print('共出現幾次? :')
print(value)

s = min(result_dict.values())
for item in result_dict.items():
value =item[1]
if value == s:
key = item[0]
print('1~6哪一個點數出現最少次:')
print(key)
print('共出現幾次? :')
print(value)

for i in range (1,7):
print(i,": 出現",result_dict[i]+"次")

1

iT邦高手 1 級 ‧ 2021-05-24 17:43:46

``````import random

r = [0 for _ in range(6)]

for _ in range(100):
r[random.randint(0, 5)] += 1

for i, j in enumerate(r):
print(f"{i + 1}: 出現 {j} 次")

print(f"{' '.join([str(i+1) for i, x in enumerate(r) if x == max(r)])} 出現最多次，是{max(r)}次")
print(f"{' '.join([str(i+1) for i, x in enumerate(r) if x == min(r)])} 出現最少次，是{min(r)}次")
``````
``````1: 出現 18 次
2: 出現 15 次
3: 出現 14 次
4: 出現 20 次
5: 出現 19 次
6: 出現 14 次
4 出現最多次，是20次
3 6 出現最少次，是14次
``````

0
johnsonnnn
iT邦新手 5 級 ‧ 2021-05-24 18:16:38
``````import pandas as pd
import random

def change(n):
ex = [0] * 6
ex[n] = 1
return ex

data = [change(random.randint(0, 5)) for x in range(100)]
df = pd.DataFrame(data, columns=[x + 1 for x in range(6)])
print(df.sum())
``````

1
froce
iT邦大師 1 級 ‧ 2021-05-25 09:42:03

collections的Counter很好用

``````import random
from collections import Counter

numbers  = [random.randint(1,6) for i in range(100)]
counted = list(Counter(numbers).items())
counted_sort_by_key = sorted(counted, key=lambda x: x[0])
counted_sort_by_value = sorted(counted, key=lambda x: x[1])

[print("點數{0}出現{1}次".format(i[0], i[1])) for i in counted_sort_by_key]

print("點數{0}出現最少次，共{1}次".format(counted_sort_by_value[0][0], counted_sort_by_value[0][1]))
print("點數{0}出現最多次，共{1}次".format(counted_sort_by_value[-1][0], counted_sort_by_value[-1][1]))
``````
0
echochio
iT邦高手 1 級 ‧ 2021-05-25 18:05:38

``````import pandas, numpy, random
r = pandas.DataFrame(numpy.random.randint(1,7, size=(1,100))).values.tolist()[0]
for j in range(1,7):
print(f"出現 {j} 的 {len([x for x in r if x == j])} 次")
``````

``````import numpy, random
r = numpy.random.randint(1,7,100, numpy.int)
for j in range(1,7):
print(f"出現 {j} 的 {len([x for x in r if x == j])} 次")
``````
``````出現 1 的 9 次

``````

`````` print (r)
``````