Python 按字长索引

Python 按字长索引,python,dictionary,indexing,new-operator,Python,Dictionary,Indexing,New Operator,我的目标只是做一个刽子手游戏。然而,我有点过于雄心勃勃。我想让用户输入他们想要这个单词的长度。然后随机选择一个相同长度的单词。为这样长度的整个词典编制索引在每次迭代中花费的时间太长。所以我有一本字典,格式如下: 酵母多糖 发酵镜 酵母菌 我想能够输出一个文件为每个'字长'自动使用这个程序。像这样: 1letterwords.txt 2letterwords.txt 等等 我昨天开始学python。我在网上和这个网站上都搜索了一下,结果一无所获。 我想要一些关于如何从这个特定编程问题开始的建议。

我的目标只是做一个刽子手游戏。然而,我有点过于雄心勃勃。我想让用户输入他们想要这个单词的长度。然后随机选择一个相同长度的单词。为这样长度的整个词典编制索引在每次迭代中花费的时间太长。所以我有一本字典,格式如下:

酵母多糖

发酵镜

酵母菌

我想能够输出一个文件为每个'字长'自动使用这个程序。像这样:

1letterwords.txt

2letterwords.txt

等等

我昨天开始学python。我在网上和这个网站上都搜索了一下,结果一无所获。 我想要一些关于如何从这个特定编程问题开始的建议。 提前谢谢! (为了澄清这一点,刽子手游戏会在请求的字长文件中随机打开一行,大大降低性能影响。)

url = urllib.urlopen('http://download.oracle.com/javase/tutorial/collections/interfaces/examples/dictionary.txt')
random.choice([item for item in url if len(item) == 8])
e、 g


把一整本字典载入内存其实没什么大不了的。您可以尝试以下方法:

import random
from collections import defaultdict

# load words
index = defaultdict(list)
with open('words.txt') as file:
    for line in file:
        word = line.strip().lower()
        index[len(word)].append(word)

# pick a random word
length = int(raw_input('Enter word length: '))
word = random.choice(index[length])
如果您坚持按字长生成单独的文件,请在加载索引后运行以下代码,如下所示:

for length in sorted(index):
    path = 'words%d.txt' % length
    with open(path, 'w') as file:
        for word in index[length]:
            file.write('%s\n' % word)

把一整本字典载入内存其实没什么大不了的。您可以尝试以下方法:

import random
from collections import defaultdict

# load words
index = defaultdict(list)
with open('words.txt') as file:
    for line in file:
        word = line.strip().lower()
        index[len(word)].append(word)

# pick a random word
length = int(raw_input('Enter word length: '))
word = random.choice(index[length])
如果您坚持按字长生成单独的文件,请在加载索引后运行以下代码,如下所示:

for length in sorted(index):
    path = 'words%d.txt' % length
    with open(path, 'w') as file:
        for word in index[length]:
            file.write('%s\n' % word)

获取随机的文件行可能也不是您想要做的。。。将它们保存在一个列表和/或口述中应该是很好的,即使是数百万个单词

通过迭代所有单词并将其添加到列表种子defaultdict中,可以按单词长度存储单词列表:

from collections import defaultdict
import random

wordsByLength = defaultdict( list )
for word in allWords:
    wordsByLength[ len(word) ].append( word )
然后,每当您需要一个随机单词时,您可以执行以下操作:

randomLen = random.choice( wordsByLength.keys() )
randomWord = random.choice( wordsByLength[ randomLen ] )

或者,您可以使用所需的指定长度替换randomLen。

获取文件的随机行可能也不是您想要做的。。。将它们保存在一个列表和/或口述中应该是很好的,即使是数百万个单词

通过迭代所有单词并将其添加到列表种子defaultdict中,可以按单词长度存储单词列表:

from collections import defaultdict
import random

wordsByLength = defaultdict( list )
for word in allWords:
    wordsByLength[ len(word) ].append( word )
然后,每当您需要一个随机单词时,您可以执行以下操作:

randomLen = random.choice( wordsByLength.keys() )
randomWord = random.choice( wordsByLength[ randomLen ] )

或者你可以用你想要的指定长度替换randomLen。

当然,简单的方法没有那么有效,但它真的太慢了吗

In [1]: import random

In [2]: timeit words = list(open("sowpods.txt"))
10 loops, best of 3: 48.4 ms per loop

In [3]: words = list(open("sowpods.txt"))

In [4]: len(words)
Out[4]: 267751

In [5]: timeit random.choice([w for w in words if len(w.strip())==6])
10 loops, best of 3: 62.5 ms per loop

In [6]: random.choice([w for w in words if len(w.strip())==6])
Out[6]: 'NAPKIN\r\n'
单行程序版本在这台计算机上只需要不到十分之一秒的时间

In [7]: timeit random.choice([w for w in open("sowpods.txt") if len(w.strip())==6])
10 loops, best of 3: 91.2 ms per loop

In [8]: random.choice([w for w in open("sowpods.txt") if len(w.strip())==6])
Out[8]: 'REVEUR\r\n'

您可以在其上添加一个
.strip()
,以消除最后的
'\r\n'

当然,简单的方法没有那么有效,但它真的太慢了吗

In [1]: import random

In [2]: timeit words = list(open("sowpods.txt"))
10 loops, best of 3: 48.4 ms per loop

In [3]: words = list(open("sowpods.txt"))

In [4]: len(words)
Out[4]: 267751

In [5]: timeit random.choice([w for w in words if len(w.strip())==6])
10 loops, best of 3: 62.5 ms per loop

In [6]: random.choice([w for w in words if len(w.strip())==6])
Out[6]: 'NAPKIN\r\n'
单行程序版本在这台计算机上只需要不到十分之一秒的时间

In [7]: timeit random.choice([w for w in open("sowpods.txt") if len(w.strip())==6])
10 loops, best of 3: 91.2 ms per loop

In [8]: random.choice([w for w in open("sowpods.txt") if len(w.strip())==6])
Out[8]: 'REVEUR\r\n'


您可以在其上添加一个
.strip()
,以消除末尾的
'\r\n'

欢迎使用堆栈溢出!请发布一些代码来演示您迄今为止所做的尝试。这将使我们更容易提出建议。这是问题的一部分哈哈,作为一个初学者,我想我甚至没有连枷的背景。我的代码所做的就是打开文件…这是一个令人悲伤的状态。欢迎使用堆栈溢出!请发布一些代码来演示您迄今为止所做的尝试。这将使我们更容易提出建议。这是问题的一部分哈哈,作为一个初学者,我想我甚至没有连枷的背景。我的代码所做的就是打开文件…这是一种令人悲伤的状态。为什么每次你想要一个随机单词时都会点击URL?我更希望他有自己的单词词典,第二行是更重要的一行是的,我有一本单词词典。如果我每次都运行它,它会非常大,而且需要永远打开。为什么每次你想要一个随机单词时都会点击URL?我更希望他有自己的单词词典,第二行是更重要的一行是的,我有一本单词词典。如果我每次都运行它的话,它会非常大,而且需要永远打开。如果我想让它更具交互性,我可以说使用file=raw_输入(目录),然后使用“with open(file)as file”吗?第二个问题:我如何运行你的代码?我得到了错误“index[len(word)]。append(word)”对不起,我有一个打字错误,后来有人纠正了。。。请参阅已编辑的代码。如果我希望它更具交互性,我是否可以说使用file=raw_输入(目录),然后使用“with open(file)as file”?第二个问题:我如何运行你的代码?我得到了错误“index[len(word)]。append(word)”对不起,我有一个打字错误,后来有人纠正了。。。见编辑代码。宾果,我现在明白了:P谢谢!宾果,我现在明白了P谢谢!