Это входной 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)
output
Laptops are dell, asus, and acer. Mob are mi, and realme. Bus are and aB. Cars are and not found.
В приведенном выше предложении в шине есть только одно значение, то есть aB, но в сгенерированном я получил его как 'bus are and aB', а для автомобилей это сгенерировано как "cars not found"
Но ожидаемый результат должен быть таким, как показано ниже:
Laptops are dell, asus, and acer. Mob are mi, and realme. Bus is aB. Cars are not found.
где Bus is 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, у меня значение автомобилей пусто, и вывод показан ниже
вывод
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