简体   繁体   English

如何将dict项目转换为句子

[英]how to convert dict items into sentence

I have a input data with two dict index and notes.我有一个带有两个字典索引和注释的输入数据。

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"
         ]

}

Created a sentence generator创建了一个句子生成器

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="") 

with the help of SenGen, i generated a sentence which is shown below.在 SenGen 的帮助下,我生成了一个如下所示的句子。

SenGen(Notes,indexes)

output output

Laptops are dell, asus, and acer. Mob are mi, and realme. Bus are and aB. Cars are and not found. 

in the above output in laptops i have 3 words which is seprated by 'and' ,**In mob i have two words which is separated by 'and' **.While in the Bus i have only one word.在上面的 output笔记本电脑中,我有 3 个单词,用“and”分隔,**在 mob 中,我有两个单词,用“and”分隔 **。在公共汽车中,我只有一个单词。 but 'and' is front of aB , and in thecase of its same cars i have not found .但是“和”在 aB 的前面,如果是同一辆车,我还没有找到

my desired output should be like shown below.我想要的 output 应该如下所示。

output: output:

Laptops are dell, asus, and acer. Mob are mi, and realme. Bus are aB. Cars are  not found. 

i followed set of cases to fix that issue shown below.我遵循了一系列案例来解决如下所示的问题。

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="")

but im unable to solve it.但我无法解决它。

You could solve your problem by relying on lists and its methods in Python:您可以依靠 Python 中的列表及其方法来解决您的问题:

keys = list(Notes.keys())
keys.remove('indexs')

for key in keys:
    first_clause = ', '.join(Notes[key][:-1])
    last_clause = Notes[key][-1:][0]
    
    if len(first_clause) == 0:
        print(key + ' is ', last_clause, end='. ')
    else:
        print(key + ' are ', first_clause, ', and ', last_clause, end='. ')

Then your output is:那么你的 output 是:

Laptops are  dell, asus , and  acer. Mob are  mi , and  realme. Bus is  aB. Cars is  Not found. 

Validate before.之前验证。

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="")
                if len(b) == 1:
                    print(f"{e}. ", end="")
                else:
                    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="")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM