简体   繁体   中英

python: Is it possible to create a list inside of a function?

Is it possible to create a list inside of a function? I have some commands I'd like to consolidate into a new function (runTimes). The code below works and does what I want it to do. I am having trouble turning it into a function.

import numpy as np
import random

numRounds = 10
numTimes = 5
finalList = []


# First Function
def runRounds(numberOfRounds):
    for xRound in range(numberOfRounds):
        if random.randint(0,100) >= 85:
            firstList.append(1)
        else:
            firstList.append(0)
    finalList.append(max(firstList))


# Run some # of times
for time in range(numTimes):
    firstList = []
    runRounds(numRounds)
    print firstList


print finalList

I cannot just indent my code to create a usable function. In the example below, I try to do this and it fails because 'firstList' is never actually defined I presume. I get the following error:

NameError: global name 'firstList' is not defined

This is an abbreviated example of my code, but it illustrates my problem. I need to be able to append finalList with a single value derrived from firstList every time in the loop. The way I do this is by defining 'firstList = []' before executing runRounds each time so that firstList will be empty every time runRounds executes. But if I try to create firstList inside a new function it will not work. I try that in the below example. I bet there is a standard way to do what I need done that I don't know about, I'm still new- thanks for the patience...

import numpy as np
import random

numRounds = 10
numTimes = 5
finalList = []


# First Function
def runRounds(numberOfRounds):
    for xRound in range(numberOfRounds):
        if random.randint(0,100) >= 85:
            firstList.append(1)
        else:
            firstList.append(0)
    finalList.append(max(firstList))


# Run some # of times
def runTimes(numberofTimes):
    for time in range(numTimes):
        firstList = []
        runRounds(numRounds)
        print firstList


runTimes(numTimes)

print finalList

Variables created in functions are not global, so you can't access them at any time.

Here is code that will work

import numpy as np
import random

numRounds = 10
numTimes = 5
finalList = []

def runRounds(numberOfRounds, firstList, finalList):
    for xRound in range(numberOfRounds):
        if random.randint(0,100) >= 85:
            firstList.append(1)
        else:
            firstList.append(0)
    finalList.append(max(firstList))
    return (firstList, finalList)

def runTimes(numTimes, numRounds, finalList):
    for time in range(numTimes):
        firstList = []
        firstList, finalList = runRounds(numRounds, firstList, finalList)
        print(firstList)
    return (firstList, finalList)

firstList, finalList = runTimes(numTimes, numRounds, finalList)

print(finalList)

When you run runRounds you need to pass in the lists as parameters because you aren't creating any in the function. At the end of it, you need to return the lists, so they can be accessed later.

For runTimes you need to pass in the numRounds variable you created earlier and the finalList because you are creating firstList in the function. You have to return both of these so they can be accessed later,

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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