根据条件造句

2024-09-25 08:38:58 发布

您现在位置:Python中文网/ 问答频道 /正文

这是输入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

Tags: andifabcarsaredellendmob
1条回答
网友
1楼 · 发布于 2024-09-25 08:38:58

你做错了的是你从来没有考虑过某些情况。 这是函数的修改版本;我写了一些评论来解释你忘记的案例:

def SenGen(alpha,beta):
    for a,b in alpha.items():
        for c,d in beta.items():
            if c in a:
                # in case the value of some key is empty (like Cars in your case)
                if b == []:
                    print(f"{a} are not found.")
                if len(b) > 1:
                    print(f"{a} are ", end="")
                for i, e in enumerate(b):
                    # in case the key has only one value, which might be "Not found" or some other value
                    if len(b) == 1:
                        if "Not found" in b:
                            print(f"{a} are {e}. ", end="")
                        else:
                            print(f"{a} is {e}. ", end = "")
                    # in case the key has more than one value
                    else:
                        if i == len(b)-1:
                            print(f"and {e}. ", end="")
                        elif i == len(d)-2:
                            print(f"{e} ", end="")
                        else:
                            print(f"{e}, ", end="")

相关问题 更多 >