简体   繁体   中英

How to deal with different size lists in python and indexes (List indexes)

Im writing a python script and am confused on some list related logic. I have 2 lists prizeList which contains prize elements, and a winners list containing winner elements. For every element in the winnerList I want to associate a prize element with it.

I have two methods: distributePrizes which contains this logic and sendEmail method which is called from distributePrizes method.

The issue I am having is that when there is not enough prizes for each winner the script stops after the first check.

There are three possible conditions: 1. Enough Prizes for the number of winners. In this case call emailWinners method. 2.Less prizes than there are winners. Call emailWinners for every winner thats matched with a prize. 3. No prizes. In this case output an error.

I am unsure how to call the emailWinners method for every winner matched with a prize. I get index out of bounds errors when trying to call emailWinner for every winner when the prizeList and winnerList are of different sizes.

eg(winnerList[] = length 5, prizeList[] = length 3. Should email 3 winners with prize but instead gets an index out of bounds error.)

Here's what I have tried so far:

 if len(prizeArray) < len(winnerEmail):

    # Not enough prizes for every winner

    print("Not enough prizes for " + prizeType)  # Alert if not enough prizes

    for email in winnerEmail:
        emailUserWithPrize(winnerEmailAddress, winnerPrize)

winnerEmailAddress = ""
winnerPrize = ""
for i in range(len(winnerEmail)):

    # For every prize thats available, assign one email to it

    winnerEmailAddress = winnerEmail[i]

    # Assign an email from the list to a prize from the list

    if i < len(prizeArray):

        winnerPrize = prizeArray[i]

        prizeArray.remove(winnerPrize)

        # Write array content to prize file, essentially removing used prizes

        openFile.close()  # Should delete all content

        writeToFile = open(prizeFile, 'w')

        writeToFile.write(prizeArray[i])  # Should write remaining prizes back to file





    else:

        print("No prize available for " + winnerEmail[i])

    # print(winnerEmailAddress, " won ", winnerPrize)

    # also need to remove this entry from prize file

    emailUserWithPrize(winnerEmailAddress, winnerPrize)

You would typically use the zip function for stuff like this. For example:

import re
import random
p = re.compile(r'^\w+@\w+\.\w+$')

prizes = ['blender', 'car', 'pencils', 'tablet']
emails = ['', 'sjadhgf', 'bob@bob.com', 'jack@bob.com',
          'jenny@bob.com', 'frank@google.com', 'someone@gmail.com',
          'other@other.com', 'runner@xxx.co']

valid_emails = [e for e in emails if p.match(e)]
random.shuffle(valid_emails)

for winner, prize in zip(valid_emails, prizes):
    print(f'{prize} goes to {winner}')

That is expected behaviour, since the check

if len(prizeList) > len(emailList):

Checks whether the prizelist is larger than the email list. This if statement has no else statement and thus the script stops when len(prizelist) <= len(emailList)

Whoever wrote this, probably applied this check so that the assumption "there are enough prizes for everyone" can be made from that point on.

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