这是输入Dict,我从中导出并生成了一个句子
输入
indexes={'Laptops':'1','Mob':'2','Cars':'3','Bus':4}
Notes={
'indexs':[1,3],
'Laptops':[
"dell","asus","acer"
],
'Mob':[
"mi","realme"
],
'Bus':[
"aB"
],
'Cars':["Not found"
]
}
创建了一个句子生成器以生成句子:
def SenGen(alpha,beta):
for a,b in alpha.items():
for c,d in beta.items():
if c in a:
print(f"{a} are ", end="")
for i, e in enumerate(b):
if i == len(b)-1:
print(f"and {e}. ", end="")
elif i == len(d)-2:
print(f"{e} ", end="")
else:
print(f"{e}, ", end="")
在这个函数的帮助下,我生成了一个句子
SenGen(Notes,indexes)
输出
Laptops are dell, asus, and acer. Mob are mi, and realme. Bus are and aB. Cars are and not found.
在上面的句子中,Bus中只有一个值,即aB,但在生成的中,我将其作为“Bus are和aB”,对于汽车,它生成为“未找到汽车”
但预期输出应如下所示:
Laptops are dell, asus, and acer. Mob are mi, and realme. Bus is aB. Cars are not found.
公共汽车是aB型,汽车不是
请帮助我编写代码,以及如何为案例1即兴编写代码
案例2:
input indexes={'Laptops':'1','Mob':'2','Cars':'3','Bus':4}
Notes={
'indexs':[1,3],
'Laptops':[
"dell","asus","acer"
],
'Mob':[
"mi","realme"
],
'Bus':[
"aB"
],
'Cars':[
]
}
在案例2中,我知道cars的值为空,输出如下所示
输出
Laptops are dell, asus, and acer. Mob are mi, and realme. Bus are and aB. Cars are
期望输出:
Laptops are dell, asus, and acer. Mob are mi, and realme. Bus is aB. Cars are not found.
请帮助我编写代码,以及如何即兴编写案例2的代码
我厌倦了以下情况:
1.if i == len(b)==1:
print(f" is {e} ", end="")
2.if i == len(b)==0:
print(f" is {e} ", end="")
3.if i == len(b)<1:
print(f" is {e} ", end="")
4.if i == len(b)>1:
print(f" is {e} ", end="")
我的输出没有变化,我的输出保持如下
Laptops are dell, asus, and acer. Mob are mi, and realme. Bus are and aB. Cars are
你做错了的是你从来没有考虑过某些情况。 这是函数的修改版本;我写了一些评论来解释你忘记的案例:
相关问题 更多 >
编程相关推荐