在列表中查找最大值

时间:2015-07-31 18:30:30

标签: python list

我是Python编程的新手,我已经被一位在线朋友负责编写代码来解决以下问题:

'想象一下棋盘游戏,你必须滚动2个骰子。写一个程序滚动骰子100次,找出哪个值(两个骰子)最多出现'

我的尝试低于某种意义,因为我能够确定两个骰子面部的最大频率加在一起,而不是实际掷出的骰子。(例如,总数' 9'是最频繁抛出的。)

我确信有很多方法可以实现上述目标,所以请原谅我第一次尝试编码!

import random
results = []

freq_2 = 0
freq_3 = 0
freq_4 = 0
freq_5 = 0
freq_6 = 0
freq_7 = 0
freq_8 = 0
freq_9 = 0
freq_10 = 0
freq_11 = 0
freq_12 = 0

for i in range(100):
    face1 = random.randrange(1,7)
    face2 = random.randrange(1,7)
    value = face1 + face2
    if value == 2:
        freq_2 += 1
    if value == 3:
        freq_3 += 1
    if value == 4:
        freq_4 += 1
    if value == 5:
        freq_5 += 1
    if value == 6:
        freq_6 += 1
    if value == 7:
        freq_7 += 1
    if value == 8:
        freq_8 += 1
    if value == 9:
        freq_9 += 1
    if value == 10:
        freq_10 += 1
    if value == 11:
        freq_11 += 1
    if value == 12:
        freq_12 += 1

results.append(freq_2)
results.append(freq_3)
results.append(freq_4)
results.append(freq_5)
results.append(freq_6)
results.append(freq_7)
results.append(freq_8)
results.append(freq_9)
results.append(freq_10)
results.append(freq_11)
results.append(freq_12)

print max(results)
print freq_2, freq_3, freq_4, freq_5, freq_6, freq_7, freq_8, freq_9,        freq_10, freq_11, freq_12

5 个答案:

答案 0 :(得分:2)

collections提供了Counter,这使得此任务变得简单:

from random import choice
from collections import Counter

dice = range(1,7)
freq = Counter([choice(dice) + choice(dice) for i in range(100)])

print freq
print freq.most_common(1)

答案 1 :(得分:1)

我会重做很多,减少你正在使用的变量数量。

而不是每个freq_#的单独变量,请使用列表:

freqs = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] # 12 zeroes
for i in range(100):
    face1 = random.randrange(1,7)
    face2 = random.randrange(1,7)
    value = face1 + face2
    freqs[value] += 1

这样,您也不必在事后将每个freq_#单独附加到列表中。

一旦这个列表填充了它的值,你可以使用一些简单的python调用来查找你喜欢的数据:

  

'最频繁的投掷是9次,发生了21次'

最频繁的投掷将是列表中编号最高的索引。

max_freq = max(freqs) # amount of times this number was thrown

滚动的数字将由max

的索引表示
most_frequent_roll = freqs.indexOf(max_freq) # the number that was thrown that many times.

答案 2 :(得分:0)

您不需要明确写出所有案例。对于这样的任务,python dictionaries非常有用。

我不会为你解决问题,但是提示你如何使用字典来实现这个:

# define a dictionary to hold the counts (just for a single dice here)
counts = {nbr_dots: 0 for nbr_dots in range(1, 7)}
# this will look like {1:0, 2:0, ...}

# now whenever you get a certain count (say 2 here) you can increment the value of 
# this count by 1 like so:
counts[2] += 1
# now counts looks like: {1:0, 2:1, ...}

如果那时你想获得最多出现的关键(那么计数):

most_frequent = max(counts, key=lambda k: counts[k])
# and the number of times it appeared:
nbr_observations = counts[most_frequent]

希望这个最小的例子有助于您入门。

答案 3 :(得分:-1)

其他答案都很好,但如果由于某种原因你不想使用它们,那么这是一个简单的循环,可以在计算结果后完成工作。

maximum = 0
for i in range(len(results)):
    if results[i] > maximum:
        maximum = results[i]
        itemAt = i
print('The most frequent throw was: '+ str(results[itemAt]))
print('It occured %d times' % maximum)

答案 4 :(得分:-1)

要找到每个骰子的值,你必须做的是你必须编写另一个for循环,记录每个面的值是否为某个数字,然后增加该值的频率:

face_freq_2 = 0
face_freq_3 = 0
face_freq_4 = 0
face_freq_5 = 0
face_freq_6 = 0
face_freq_7 = 0
face_freq_8 = 0
face_freq_9 = 0
face_freq_10 = 0
face_freq_11 = 0
face_freq_12 = 0

for j in range(100):

face_value1 = random.randrange(1,7)
face_value2 = random.randrange(1,7)
value1 = face_value1
value2 = face_value2


if (value1 == value2) and (value1 == 1):
    face_freq_2 += 1
if (value1 == 1 and value2 == 2) or (value1 == 2 and value2 == 1):
    face_freq_3 += 1
if (value1 == value2) and (value1 == 2):
    face_freq_4 += 1
elif (value1 == 1 and value2 == 3) or (value1 == 3 and value2 == 1):
    face_freq_4 += 1
if (value1 == 1 and value2 == 4) or (value1 == 4 and value2 == 1):
    face_freq_5 += 1
elif (value1 == 2 and value2 == 3) or (value1 == 3 and value2 == 2):
    face_freq_5 += 1
if (value1 == value2 and value1 == 3):
    face_freq_6 += 1
elif (value1 == 1 and value2 == 5) or (value1 == 5 and value2 == 1):
    face_freq_6 += 1
elif (value1 == 2 and value2 == 4) or (value1 == 4 and value2 == 2):
    face_freq_6 += 1

从中你可以了解你必须做的事情,因为随着值的增加,除了初始的if语句之外,你还需要更多的elif语句。是的,这有点乏味但它应该产生预期的结果。