如何使用python中的函数生成2个字母的单词?

2024-10-01 13:23:53 发布

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

嗨,所以我使用python,我正在尝试创建一个函数,让我生成由2个字母组成的单词。我还想数一数字典里到底有多少单词。在

到目前为止,我得到的是:

alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o',
            'p','q','r','s','t','u','v','w','x','y','z')
count1 = 0
text = " "

def find2LetterWords():
    for letter in alphabet:
        text += letter
        for letter in alphabet:
            text +=letter
    print text

这是我迄今为止编写的代码,我知道它不对。我只是在做实验。所以如果你能帮我,那就太好了。谢谢。在


Tags: 函数代码textinfor字典def字母
3条回答

来自itertools模块的product正是生成所有可能的2个字母单词的列表所需的内容。在

from itertools import product

alphabet = ('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z')

two_letter_words = product(alphabet, alphabet)

for word in two_letter_words:
    print word

为了比较字典中的哪一个,你需要从其他地方得到

根据评论编辑的答案:

def find2LetterWords():
     #this generates all possible 2-letter combos with a list comprehension
     words = [first + second for second in alphabet for first in alphabet]
     #create a new list with only those words that are in your_dictionary (a list)
     real_words = [word for word in words if word in your_dictionary]
     return real_words

如果你想要一个漂亮的没有功能的一行:

^{pr2}$

显然,将your_dictionary替换为字典的任何名称。在

另一种方法,通过列表理解:

words = [x+y for x in alphabet for y in alphabet]

或者自己不打字母表:

^{pr2}$

让我们比较一下xvatar,Toote和我的答案:

from itertools import product
from string import ascii_lowercase as a
import timeit

def nestedFor():
    w = []
    for l1 in a:
        for l2 in a:
            word = l1+l2
            w.append(word)
    return w

def nestedForIter():
    w = []
    for l1 in a:
        for l2 in a:
            yield l1+l2

def withProduct():
    return product(a,a)

def listComp():
    return [x+y for x in a for y in a]

def generatorComp():
    return (x+y for x in a for y in a)

# return list
t1 =  timeit.Timer(stmt="nestedFor()",
                   setup = "from __main__ import nestedFor")
t2 = timeit.Timer(stmt="list(withProduct())",
                   setup = "from __main__ import withProduct")
t3 = timeit.Timer(stmt="listComp()",
                   setup = "from __main__ import listComp")

# return iterator
t4 = timeit.Timer(stmt="nestedForIter()",
                   setup = "from __main__ import nestedForIter")
t5 = timeit.Timer(stmt="withProduct()",
                   setup = "from __main__ import withProduct")
t6 = timeit.Timer(stmt="generatorComp()",
                   setup = "from __main__ import generatorComp")

n = 100000

print 'Methods returning lists:'
print "Nested for loops:   %.3f" % t1.timeit(n)
print "list(product):      %.3f" % t2.timeit(n)
print "List comprehension: %.3f\n" % t3.timeit(n)

print 'Methods returning iterators:'
print "Nested for iterator:     %.3f" % t4.timeit(n)
print "itertools.product:       %.3f" % t5.timeit(n)
print "Generator comprehension: %.3f\n" % t6.timeit(n)

结果:

Methods returning lists:
Nested for loops: 13.362
list(product): 4.578
List comprehension: 7.231

Methods returning generators:
Nested for iterator: 0.045
itertools.product: 0.212
Generator comprehension: 0.066

换句话说,如果您确实需要一个完整的列表,一定要使用itertools.product。但是,生成器速度更快,需要的内存更少,可能就足够了。在

相对缓慢itertools.product由于迭代器是意外的,考虑到the documentation表示它与生成器表达式中的嵌套for循环等价。似乎有一些开销。在

相关问题 更多 >