简体   繁体   English

在python中生成随机句子

[英]Generate random sentences in python

I'm very new to python and programming!我对python和编程很陌生!

I need to write a program that has 4 tuples with 5 elements each.我需要编写一个程序,它有 4 个元组,每个元组有 5 个元素。 One tuple should have verbs, one tuple should have nouns, one tuple should have adjectives, and one tuple should have adverbs.一个元组应该有动词,一个元组应该有名词,一个元组应该有形容词,一个元组应该有副词。 Then I have to use a randomly generated numbers between 0 and 4 to pick one of the elements from each tuple.然后我必须使用 0 到 4 之间随机生成的数字来从每个元组中选择一个元素。 This is what I have so far:这是我到目前为止:

import random

nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs") 
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")
num = random.randrange(0,5)
print (num)

Can someone show me what I'm doing wrong?有人可以告诉我我做错了什么吗?

You can use random.choice within a list comprehension then concatenate the selected list with join :您可以在列表理解中使用random.choice然后将所选列表与join

>>> l=[nouns,verbs,adj,adv]
>>> ' '.join([random.choice(i) for i in l])
'girl runs dirty crazily.'
>>> ' '.join([random.choice(i) for i in l])
'monkey hits clueless occasionally.'

The easiest way for your understanding:最简单的理解方法:

import random

nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs") 
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")
num = random.randrange(0,5)
print nouns[num] + ' ' + verbs[num] + ' ' + adv[num] + ' ' + adj[num]

For a similar function to the accepted answer that returns all possible permutations:对于返回所有可能排列的已接受答案的类似函数:

import random

def make_random_sentence():
  nouns = ["puppy", "car", "rabbit", "girl", "monkey"]
  verbs = ["runs", "hits", "jumps", "drives", "barfs"]
  adv = ["crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally."]
  adj = ["adorable", "clueless", "dirty", "odd", "stupid"]

  random_entry = lambda x: x[random.randrange(len(x))]
  return " ".join([random_entry(nouns), random_entry(verbs), random_entry(adv), random_entry(adj)])

If you want to select always the same index for each tupple, then try something like this:如果要为每个元组选择始终相同的索引,请尝试以下操作:

import random
idx = random.randrange(0, 5)
print ("%s %s %s %s"%(nouns[idx], verbs[idx], adv[idx], adj[idx]))

If you want to get random elements for each tupple, the following should work:如果你想为每个元组获取随机元素,以下应该有效:

import random
print("%s %s %s %s"%(random.choice(nouns), random.choice(verbs), random.choice(adv), random.choice(adj))
num = random.randrange(0,5)   
print (num)

Just gives you the random number you generated.只是给你你生成的随机数。 You need to acess the tuples using that random number you generated like this.您需要使用像这样生成的随机数来访问元组。

print(nouns[num])
print(verbs[num])
print(adv[num])
print(adj[num])

I would do as follows, using random.choice because it's a better choice than random.randrange...我会做如下,使用 random.choice 因为它是一个比 random.randrange 更好的选择......

words = (adj, nouns, verb, adv)
sentence = "".join(choice(word) for word in words)

random.choice is to select a random thing from a list, tuple or similar container. random.choice是从列表、元组或类似容器中随机选择一个东西。 What the last line does is to select one random thing from each list, and make a new list from the choices.最后一行的作用是从每个列表中随机选择一个事物,并从选择中创建一个新列表。 Then that list is printed.然后打印该列表。 And being in a loop, this is done X number of times, whatever number the user inputs.在循环中,无论用户输入什么数字,都会执行 X 次。

from random import choice 

nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs") 
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")

# asking user as to how many sentences he would like to generate 
for _ in range (int (input ("Enter integer value :"))): 
    print(list(map(choice, [nouns, verbs, adv, adj])))
Enter integer value :2
['car', 'barfs', 'dutifully.', 'odd']
['rabbit', 'runs', 'occasionally.', 'clueless']

[Program finished] 

This Is A Simple One, If You Want Multiple Sentences Possible, Use This:这是一个简单的,如果你想要多个句子可能,使用这个:

nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs") 
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")

print(nouns[random.randrange(0,5)] + ' ' + verbs[random.randrange(0,5)] + ' ' 
+ adv[random.randrange(0,5)] + ' ' + adj[random.randrange(0,5)])
import random
nouns = ("puppy", "car", "rabbit", "girl", "monkey")
verbs = ("runs", "hits", "jumps", "drives", "barfs") 
adv = ("crazily.", "dutifully.", "foolishly.", "merrily.", "occasionally.")
adj = ("adorable", "clueless", "dirty", "odd", "stupid")
num = random.randrange(0,5)
print(nouns[num],verbs[num],adv[num],adj[num])

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

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