我想列印一個句子,其中 2 個單詞會根據字典中的某些條件發生變化。
如果名詞以字母 a 結尾,那么在句子中使用任何帶有陰性和單數值的“any”鍵的冠詞。基本上我想在所有鍵中搜索任何“女性”和“單數”。一旦找到具有“female”和“singular”的鍵,則回傳鍵“la”
article = {
"il" : {
"gender" : "male",
"unit" : "singular"
},
"la" : {
"gender" : "female",
"unit" : "singular"
},
"le" : {
"gender" : "female",
"unit" : "plural"
},
}
名詞 = { "torta" : { "genere" : "female", "unità" : "singular" } }
if noun["noun"].endswith('a'):
article is ['gender'] == ['female'] and ['unit'] == ['singular']
text = (f"{''.join(article)} {noun} {è buona}")
print(text)
uj5u.com熱心網友回復:
有些字串是西班牙語,有些是英語,這導致匹配主題和文章時出現問題。原始代碼也有一些小錯誤(例如,將字典傳遞給 f 字串)。糾正這些錯誤,我們通過回圈得到輸出(小寫字母)article_words.items():
#article
article_words = {
"il" : {
"gender" : "male",
"unit" : "singular"
},
"la" : {
"gender" : "female",
"unit" : "singular"
},
"le" : {
"gender" : "female",
"unit" : "plural"
},
}
#noun
nouns = {
"torta" : {
"gender" : "female",
"unit" : "singular"
}
}
subject = "torta"
if subject.endswith('a'):
article = ""
for key, value in article_words.items():
if nouns[subject] == value:
article = key
break
text = f"{article} {subject} è buona"
print(text)
要擺脫名詞字典,您可以執行以下操作(請參閱評論進行討論):
endings = {
"a" : {
"gender" : "female",
"unit" : "singular"
}
}
subject = "torta"
ending = None
for key, value in endings.items():
if subject.endswith(key):
ending = key
break
if ending:
article = ""
for key, value in article_words.items():
if endings[ending] == value:
article = key
break
text = f"{article} {subject} è buona"
print(text)
uj5u.com熱心網友回復:
如果您重組文章字典,例如:
article = { 'female': { 'singular': 'la', 'plural': 'le' },
'male': { 'singular': 'il' }}
你可以像這樣使用它:
word = "torta"
if word.endswith('a'):
article_ = article['female']['singular']
print(f"{article_} {word} e buena")
如果你想使用noun字典,你可以使用:
article_ = article[nount[word]['gender']][noun[word]['unita']]
article_words按原樣使用:
article = next(filter(lambda x: article_words[x]['gender'] == 'female' and
article_words[x]['unit'] == 'singular', article_words.keys()))
會給你文章 'la'
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/410058.html
標籤:
下一篇:如何從字典中復制類屬性?
