Home > Software engineering >  Is there any way to cut down this count with loops instead of 21 if statements?
Is there any way to cut down this count with loops instead of 21 if statements?

Time:04-08

I am trying to get a more efficient way of running this code without the 21 if statements. The program does need two functions, the main() and counting() and the output is supposed to be "2". Can this be done with lists or any other more efficient way while keeping the two functions? All of the constants in the first half need to stay the same. Every time I've tried using lists I get errors

def main():

    Word1 = "This"
    Word2 = "is"
    Word3 = "a"
    Word4 = "beautiful"
    Word5 = "and"
    Word6 = "sunny"
    Word7 = "day"
    Sentiment1 = "Beautiful"
    Sentiment2 = "beautiful"
    Sentiment3 = "sunny"
    

    #List = [Word1, Word2, Word3, Word4, Word5, Word6, Word7, Sentiment1, Sentiment2, Sentiment3]
    count = counting(Word1, Word2, Word3, Word4, Word5, Word6, Word7, Sentiment1, Sentiment2, Sentiment3)
    #List = [Word1, Word2, Word3, Word4, Word5, Word6, Word7, Sentiment1, Sentiment2, Sentiment3]
    totalcount = counting(Word1, Word2, Word3, Word4, Word5, Word6, Word7, Sentiment1, Sentiment2, Sentiment3)
    #print(count)
    print(totalcount)

def counting(Word1, Word2, Word3, Word4, Word5, Word6, Word7, Sentiment1, Sentiment2, Sentiment3):
    count = 0
    if Word1 == Sentiment1:
        count = count   1
    if Word1 == Sentiment2:
        count = count  1
    if Word1 == Sentiment3:
        count = count  1
    if Word2 == Sentiment1:
        count = count   1
    if Word2 == Sentiment2:
        count = count  1
    if Word2 == Sentiment3:
        count = count  1
    if Word3 == Sentiment1:
        count = count   1
    if Word3 == Sentiment2:
        count = count  1
    if Word3 == Sentiment3:
        count = count  1
    if Word4 == Sentiment1:
        count = count   1
    if Word4 == Sentiment2:
        count = count  1
    if Word4 == Sentiment3:
        count = count  1
    if Word5 == Sentiment1:
        count = count   1
    if Word5 == Sentiment2:
        count = count  1
    if Word5 == Sentiment3:
        count = count  1
    if Word6 == Sentiment1:
        count = count   1
    if Word6 == Sentiment2:
        count = count  1
    if Word6 == Sentiment3:
        count = count  1
    if Word7 == Sentiment1:
        count = count   1
    if Word7 == Sentiment2:
        count = count  1
    if Word7 == Sentiment3:
        count = count  1  
        return(count)
            #if x == Sentiment2:
                #count = count  1
                #if x == Sentiment3:
                    #count = count   1

    #print(count)
    return(count)

main()
                    
    

My second attempt using lists and loops:

def main():

    Word1 = "This"
    Word2 = "is"
    Word3 = "a"
    Word4 = "beautiful"
    Word5 = "and"
    Word6 = "sunny"
    Word7 = "day"
    Sentiment1 = "Beautiful"
    Sentiment2 = "beautiful"
    Sentiment3 = "sunny"

    List1 = [Word1, Word2, Word3, Word4, Word5, Word6, Word7, Sentiment1, Sentiment2, Sentiment3]
    sentiment = [Sentiment1, Sentiment2, Sentiment3]
    counting = [Word1, Word2, Word3, Word4, Word5, Word6, Word7, Sentiment1, Sentiment2, Sentiment3]
    #List = [Word1, Word2, Word3, Word4, Word5, Word6, Word7, Sentiment1, Sentiment2, Sentiment3]
    
    #print(totalcount)


    
    def counting():
            count = 0
            for sentiment in List1:
                    count = count   1

            #return(totalcount)


            

            #print(totalcount)
            print(count)
        

    counting() 
main()
                    
    

CodePudding user response:

words = [
    "This",
    "is",
    "a",
    "beautiful",
    "and",
    "sunny",
    "day"
]

sentiments = [
    "Beautiful",
    "beautiful",
    "sunny"
]


def counting(words, sentiments):
    return sum(1 for word in words if word in sentiments)

print(counting(words, sentiments))

CodePudding user response:

words = ["This", "is", "a", "beautiful", "and", "sunny", "day"]
sentiments = ["Beautiful", "beautiful", "sunny"]
count = 0

for word in words:
        count  = sentiments.count(word)

CodePudding user response:

This is only a slight variation on what has already been presented. It takes advantage of the fact that True counts as 1 in a sum function.

def main():
    Word1 = "This"
    Word2 = "is"
    Word3 = "a"
    Word4 = "beautiful"
    Word5 = "and"
    Word6 = "sunny"
    Word7 = "day"
    Sentiment1 = "Beautiful"
    Sentiment2 = "beautiful"
    Sentiment3 = "sunny"

    words = [Word1, Word2, Word3, Word4, Word5, Word6, Word7]
    sentiments = [Sentiment1, Sentiment2, Sentiment3]
    count = counting(words, sentiments)
    print(count)

def counting(words, sentiments):
    return sum(w in sentiments for w in words)

main()

CodePudding user response:

Combination of two answers (creating the most elegant code):

words = [
    "This",
    "is",
    "a",
    "beautiful",
    "and",
    "sunny",
    "day"
]

sentiments = [
    "Beautiful",
    "beautiful",
    "sunny"
]


def counting(words, sentiments):
    return sum(word in sentiments for word in words)

print(counting(words, sentiments))
  • Related