提问人:GabiTomoiaga 提问时间:7/22/2022 最后编辑:Hamza SayyidGabiTomoiaga 更新时间:7/22/2022 访问量:145
如何确定 6 个元素在包含 100 个元素的列表中是否相等
How to determine if 6 elements are equal in a list with 100 elements
问:
所以我有这个练习要做:
编写一个程序,找出在随机生成的正面和反面列表中出现六头条纹或六尾条纹的频率,以及是否有添加到变量中的条纹number_of_streaks
我制作了将 H 和 T 添加到列表中的循环,但我不知道如何检查该列表中是否有条纹。我尝试了这段代码:
if th[experiment_number][z] == th[experiment_number][z+1]:
但我收到此错误:IndexError: string index out of range
(注意我是编程新手,我还在学习)
import random
number_of_streaks = 0
th = []
for experiment_number in range(10000):
for x in range(100):
if random.randint(0, 1):
th.append('H')
else:
th.append('T')
first = 0
last = 5
for x in range(100):
for z in range (6):
if th[experiment_number][z] == th[experiment_number][z+1]:
number_of_streaks += 1
答:
0赞
chepner
7/22/2022
#1
浏览列表,并计算当前条纹的长度。如果当前项目等于前一个项目,则增加长度。否则,将计数器重置为 0。当计数器达到 6 时,您发现了一条条纹。
0赞
gphull
7/22/2022
#2
代码的前半部分有效,后半部分无效。
此代码应该有效:
# The number of consecutive Heads or Tails needed to form a streak
streak_num = 6
# Iterate over the list th, excluding the last 5 (streak_num-1) elements of the list
for n in range(0,len(th)-streak_num+1):
# Create a list to append values to
list = []
# Iterate streak_num times to check for a streak
for m in range(0,streak_num):
# Starting from the nth element in the list
# Append that nth element to the list and the next streak_num-1 elements to the list
list.append(th[n+m])
# Check to see if all elements in list are the same
if len(set(list)) == 1:
# If they are all the same, a streak of size streak_num has been found
# Therefore add it the count
number_of_streaks = number_of_streaks + 1
您遇到问题的部分是以下部分:
# Iterate over the list th, excluding the last 5 (streak_num-1) elements of the list
for n in range(0,len(th)-streak_num+1):
如果您正在寻找 6 的连胜,则必须停止检查列表倒数第 6 个元素。
如果您有任何问题,请告诉我。
评论
0赞
GabiTomoiaga
7/23/2022
好的,但是为什么我需要排除该列表的最后 5 个元素?
0赞
gphull
7/24/2022
您不排除列表的最后五个元素,而是停止迭代列表的最后五个元素。我的 for 循环期待接下来的 6 个元素。如果我在列表的倒数第二个元素上,并且我尝试向前看 6 个元素,我会收到错误消息“IndexError:字符串索引超出范围”。这是因为倒数第二个元素之后没有 6 个元素。只有两个,所以代码出错了。因此,循环完成对倒数第 6 个元素的迭代,并期待接下来的 5 个元素,完成对 6 条纹的搜索。
0赞
cards
7/22/2022
#3
假设条纹被描述为连续相同结果的最小数量。因此,如果它是 6,那么条纹可以由 6、7、8 组成......结果。
from itertools import groupby
from collections import Counter
import random
# initialize the random number generator - for testing!
random.seed(121)
n_throws = 100
streak_threshold = 6 # exact value or more
outcomes = 'HT'
# flip the coin
experiment = [outcomes[random.randint(0, 1)] for _ in range(n_throws)]
# streaks ids
grps_by_streak = [grp_id for grp_id, grp in groupby(experiment) if len(list(grp)) >= streak_threshold]
print(grps_by_streak)
#['H', 'H', 'T']
# frequency for each result
cc = Counter(grps_by_ streak)
print(cc)
#Counter({'H': 2, 'T': 1})
number_of_streaks = sum(cc.values())
print(number_of_streaks)
#3
如果条纹不是阈值,而是一个精确的阈值,则需要固定的小值:
- 替换为(只是为了保持一致性)
streak_threshold = 6
streak = 6
- 使用变量的相等和变化:
grps_by_streak = [grp_id for grp_id, grp in groupby(experiment) if len(list(grp)) == streak]
评论
experiment_number
x
th = []
th[experiment_number][z]...