简体   繁体   English

如何使循环重复n次-Python 3

[英]How to make a loop repeat itself n number of times - Python 3

I started programming 2 weeks ago for the first time in my life and I have come across something that I cannot figure out. 我两周前开始编程,这是我一生中的第一次,遇到了一些我无法弄清的事情。 I am trying to make it so a loop that calculates a median from a set amount of random numbers can repeat itself many times (say 10,000) while storing all the median values into a list. 我正在尝试使之循环,从而根据一组固定数量的随机数计算中位数,同时将所有中位数存储到列表中时,它可以重复多次(例如10,000次)。 I already have everything up to the point where a list is created from the random integers (numbList) from which the median (listMedian) is calculated. 我已经拥有了从随机整数(numbList)创建列表到计算中位数(listMedian)的所有内容。 I would just like to be able to repeat this process a number of times while generating a list of all the calculated medians. 我希望能够重复此过程多次,同时生成所有计算出的中位数的列表。 Sample size is how many numbers that are per list and upper limit determines what the range is for each individual number, thanks! 样本数量是每个列表有多少个数字,上限确定每个数字的范围,谢谢! I am using Python 3. 我正在使用Python 3。

import random


def median(numbList):

    srtd = sorted(numbList)
    mid = len(numbList)//2
    if len(numbList) % 2 == 0:
        return (srtd[mid-1] + srtd[mid]) / 2.0
    else:
        return srtd[mid]


sampleSize = int(input("What is your desired sample size? "))

upperLimit = int(input("What is your desired upper limit? "))

numbList = []

totalMedians = []

biggerList = []


while sampleSize > 0:

    sampleSize -= 1

    randomNum = random.randrange(0,upperLimit+1)

    numbList.append(randomNum)

    numbList.sort(key=int)

    listMedian = median(numbList)

Here's a simple example of what you want: 这是您想要的简单示例:

#!/usr/bin/python

import random

def create_list(sampleSize, upperLimit):
    numbList = []
    while sampleSize > 0:
        sampleSize -= 1
        randomNum = random.randrange(0,upperLimit+1)
        numbList.append(randomNum)
    numbList.sort(key=int)
    return numbList

def median(numList):
    list_len = len(numList)
    if list_len % 2:
        return numList[list_len / 2]
    else:
        return (numList[list_len / 2] + numList[list_len / 2 - 1]) / 2.0

def main():
    number_lists = 4
    sample_size = 5
    upper_limit = 50
    lists = []
    median_list = []

    for i in range(number_lists):
        lists.append(create_list(sample_size, upper_limit))

    for current_list in lists:
        current_median = median(current_list)
        print current_list, " : median (", current_median, ")"
        median_list.append(current_median)

    print "Median list is ", median_list

if __name__ == "__main__":
    main()

which outputs, for example: 输出如下:

paul@MacBook:~/Documents/src/scratch$ ./sample.py
[3, 18, 20, 26, 46]  : median ( 20 )
[18, 22, 38, 44, 49]  : median ( 38 )
[28, 29, 34, 42, 43]  : median ( 34 )
[4, 21, 27, 31, 46]  : median ( 27 )
Median list is  [20, 38, 34, 27]
paul@MacBook:~/Documents/src/scratch$ 

say you have this 说你有这个

print ('hello')

to loop it you would need to add : for #1 in range(#2) : at the start (#1 any variable, #2 how many times it will repeat) 要循环它,您将需要添加: for #1 in range(#2)中的#1:在开始时(#1任何变量,#2它会重复多少次)

for example 例如

for somevariable in range(3)
print('hello')

hello
hello
hello

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

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