简体   繁体   English

在 python 中生成随机句子

[英]Generating a random sentence in python

I have the following input which contains Aircraft and Services我有以下输入,其中包含飞机和服务

AirCrafts={'Cargo Aircraft':'1','International Aircraft':'2','Domestic Aircraft':'3'}
    Services={
        'AirCrafts':[1,2,3],
    
    'Cargo  Aircraft':[
        
        "Fedx","DHFL","Emirates SkyCargo","Qatar Airways Cargo","Cathay Pacific Cargo"    
    ],
    'International Aircraft':[
        "Air India","Air France","BA"
    ],
    
    'Domestic Aircraft':[
        "TruJet","Indigo","AirAsia"
    ]
}

I have a generated sentence the following sentence using SenGen Function.我使用 SenGen Function 生成了以下句子。

def SenGen(alpha,beta):
    for keys,values in alpha.items():
        for key,value in beta.items():
            if key in keys:
                if values == []:
                    print(f"{keys}  are not found.")
                if len(values) > 1:
                    print(f"{keys}  are ", end="\n")
                for i, val in enumerate(values):
                    if len(values) == 1:
                        if "Not found" in value:
                            print(f"{keys} are {val}. ", end="\n")
                        else:
                            print(f"{keys} is {val}. ", end = "\n")
                    
                    else:
                        if i == len(values)-1:
                            print(f"•{val}. ", end="\n")
                        elif i == len(value)-2:
                            print(f"•{val} ", end="\n")
                        else:
                            print(f"•{val}, ", end="\n")

My generated output is below after running SenGen(Services,AirCrafts) .运行SenGen(Services,AirCrafts)后,我生成的 output 如下。

SenGen(Services,AirCrafts)
International Aircraft  are 
•Air India, 
•Air France, 
•BA. 
Domestic Aircraft  are 
•TruJet, 
•Indigo, 
•AirAsia.

In the above output, I have international Aircraft are and Domestic Aircraft are.在上面的output中,我有国际飞机和国内飞机。 in place of International Aircraft, I want to generate a proper sentence such that my output must look like代替国际飞机,我想生成一个正确的句子,这样我的 output 必须看起来像

International Aircrafts that run from the various airport of India are从印度各个机场出发的国际飞机是

for the Domestic Aircrafts国内飞机

Domestic Aircraft which run in within India are在印度境内运行的国内飞机是

How can I generate a proper sentence as shown above?如上所示,如何生成正确的句子?

In your code, you need to plug in the additional text.在您的代码中,您需要插入附加文本。 Otherwise the program will not know that you want these texts added.否则程序将不知道您想要添加这些文本。

if len(values) > 1:
    if "International" in keys:
        print(f"{keys}  that run from the various airports of India are ", end="\n")
    elif "Domestic" in keys:
        print(f"{keys}  which run in within India are ", end="\n")
    else:
        print(f"{keys}  are ", end="\n")

Here's the code that provides you a return statement.这是为您提供返回语句的代码。

def SenGen(alpha,beta):

    temp = '' #store the results for each iteration of `AirCrafts`

    for keys,values in alpha.items():

        if keys == 'International Aircraft':

            temp += '\n' + keys + ' that run from the various airports of India are :\n' + ',\n'.join(beta[keys])

        elif keys == 'Domestic Aircraft':

            temp += '\n' + keys + ' which run within India are :\n' + ',\n'.join(beta[keys])

        elif keys == 'Cargo Aircraft':

            temp += '\n' + keys + ' are :\n' + ',\n'.join(beta[keys])

        else:

            temp += '\n' + keys + ' are not found'

        temp += '\n'

    return temp

x = SenGen(AirCrafts,Services)

print (x)

If you don't want the results to be printed separately on each line, you can remove the \n from the string.如果您不希望在每一行上单独打印结果,您可以从字符串中删除\n

Output for your reference: Output 供您参考:

>>> print (x)

Cargo Aircraft are :
Fedx,
DHFL,
Emirates SkyCargo,
Qatar Airways Cargo,
Cathay Pacific Cargo

International Aircraft that run from the various airports of India are :
Air India,
Air France,
BA

Domestic Aircraft which run within India are :
TruJet,
Indigo,
AirAsia

>>> x
'\nCargo Aircraft are :\nFedx,\nDHFL,\nEmirates SkyCargo,\nQatar Airways Cargo,\nCathay Pacific Cargo\n\nInternational Aircraft that run from the various airports of India are :\nAir India,\nAir France,\nBA\n\nDomestic Aircraft which run within India are :\nTruJet,\nIndigo,\nAirAsia\n'
>>> 

So basically this is the line which needed change as per your requirement所以基本上这是根据您的要求需要更改的行

if len(values) > 1:
   print(f"{keys} are ", end="\n")

You can change it to this, and you will be good to go:你可以把它改成这样,你会很好的 go:

if len(values) > 1:
  # for international the statement is different
  if keys == "International Aircraft": 
    print(f"{keys} that run from the various airport of India are ", end="\n")
  elif keys == "Domestic Aircraft":
    print(f"{keys}  which run in within India are", end="\n")
  else:
    # your else print

Additional Information附加信息

To return the statement, do not do like this, you need to use String Concatenation .return语句,不要这样做,您需要使用String Concatenation So in order to do the return, do this in place of print所以为了做退货,用这个代替打印

if len(values) > 1:
  # for international the statement is different
  if keys == "International Aircraft": 
    return str(keys) + " that run from the various airport of India are \n"  
  elif keys == "Domestic Aircraft":
    return str(keys) +  " which run in within India are \n"
  else:
    # your else print

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

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