简体   繁体   English

如何确定具有 100 个元素的列表中的 6 个元素是否相等

[英]How to determine if 6 elements are equal in a list with 100 eelements

So I have this exercise to do : ,,Write a program to find out how often a streak of six heads or a streak of six taiils comes up in a randomly generated list of head and tails and if there is a streak you add to to the variable number_of_steaks''所以我有这个练习要做变量 number_of_steaks''

I made the loop for adding H and T to the list but i don t know how to check if there is s streak in that list I ried this code but i get this error: if th[experiment_number][z] == th[experiment_number][z+1]: IndexError: string index out of range我创建了将 H 和 T 添加到列表中的循环,但我不知道如何检查该列表中是否有 s 条纹我试了这段代码但我收到了这个错误:if th[experiment_number][z] == th[实验编号] [z + 1]:IndexError:字符串索引超出范围

(Note I am new to programming, I am still learning) (注意我是编程新手,我还在学习)

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

Walk the list, and keep a count of the length of the current streak.遍历列表,并计算当前连胜的长度。 If the current item is equal to the previous item, increase the length.如果当前项等于前一项,则增加长度。 Otherwise, reset the counter to 0. When the counter reaches 6, you've found a streak.否则,将计数器重置为 0。当计数器达到 6 时,您就发现了条纹。

The first half of your code works, the second half doesn't.您的代码的前半部分有效,后半部分无效。

This code should work:此代码应该可以工作:

# 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
    

The part you're having trouble with is the following portion:您遇到问题的部分是以下部分:

# 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):

If you are looking for a streak of 6, you have to stop checking the list the 6th last element.如果您正在寻找连续 6 个,则必须停止检查列表的倒数第 6 个元素。

Let me know if you have any questions.如果您有任何问题,请告诉我。

Assumed that a streak is described as the least amount of consecutive identical outcomes.假设连续性被描述为最少数量的连续相同结果。 So, if it is 6 then a streak can be made of 6, 7, 8, ... outcomes.因此,如果它是 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

If a streak is not a threshold value but an exact one just smalls fixed are needed:如果一个条纹不是一个阈值,而是一个精确的,只需要固定的小值:

  • replace streak_threshold = 6 to streak = 6 (just for consistency)streak_threshold = 6替换为streak = 6 (只是为了保持一致性)
  • use equality and change of variable: grps_by_streak = [grp_id for grp_id, grp in groupby(experiment) if len(list(grp)) == streak]使用相等和变量的变化: grps_by_streak = [grp_id for grp_id, grp in groupby(experiment) if len(list(grp)) == streak]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM