我試圖在沒有 21 個 if 陳述句的情況下以更有效的方式運行此代碼。該程式確實需要兩個函式,main() 和counting(),并且輸出應該是“2”。這可以通過串列或任何其他更有效的方式來完成,同時保留這兩個功能嗎?上半場的所有常數都需要保持不變。每次我嘗試使用串列時都會出錯
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()
我第二次嘗試使用串列和回圈:
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()
uj5u.com熱心網友回復:
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))
uj5u.com熱心網友回復:
words = ["This", "is", "a", "beautiful", "and", "sunny", "day"]
sentiments = ["Beautiful", "beautiful", "sunny"]
count = 0
for word in words:
count = sentiments.count(word)
uj5u.com熱心網友回復:
這只是對已經呈現的內容的輕微變化。它利用了True在 sum 函式中計為 1 的事實。
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()
uj5u.com熱心網友回復:
結合兩個答案(創建最優雅的代碼):
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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/457679.html
