[英]Generating a random sentence in python
我有以下输入,其中包含飞机和服务
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"
]
}
我使用 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")
运行SenGen(Services,AirCrafts)
后,我生成的 output 如下。
SenGen(Services,AirCrafts)
International Aircraft are
•Air India,
•Air France,
•BA.
Domestic Aircraft are
•TruJet,
•Indigo,
•AirAsia.
在上面的output中,我有国际飞机和国内飞机。 代替国际飞机,我想生成一个正确的句子,这样我的 output 必须看起来像
从印度各个机场出发的国际飞机是
国内飞机
在印度境内运行的国内飞机是
如上所示,如何生成正确的句子?
在您的代码中,您需要插入附加文本。 否则程序将不知道您想要添加这些文本。
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")
这是为您提供返回语句的代码。
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)
如果您不希望在每一行上单独打印结果,您可以从字符串中删除\n
。
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'
>>>
所以基本上这是根据您的要求需要更改的行
if len(values) > 1:
print(f"{keys} are ", end="\n")
你可以把它改成这样,你会很好的 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
附加信息
要return
语句,不要这样做,您需要使用String Concatenation 。 所以为了做退货,用这个代替打印
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.