【问题标题】:How to generate a pyramid of words in python based on a dictionary of words?如何基于单词字典在python中生成单词金字塔?
【发布时间】:2019-12-13 21:25:47
【问题描述】:

我想使用 Python 生成一个具有以下结构的金字塔:

金字塔是使用以下方法构建的:

用户输入一个介于 1 到 15 之间的整数。然后生成请求高度的“字金字塔”。 “单词金字塔”是一串多行,以长度为 1 的单词(第一行)开始,接着是长度为 2 的单词(第二行),然后是长度为 3 的单词,以此类推,直到一个单词长度是金字塔的高度。

下一个单词与前一个单词具有相同的字母(尽管不一定按相同的顺序),并添加了一个额外的字母。

长度为 5 的金字塔如下所示:

    a
    ad
    tad
    date
    tread

单词取自英语单词列表(因此对于任何给定大小的金字塔都有足够的单词来完成金字塔)。

我尝试了以下方法:


from random import choice
from string import ascii_lowercase
alphabet = ascii_lowercase




def finding(word,target):
    chars = []
    temp_chars = []

    for char in word:
        chars.append(char)


    for char in target:
        temp_chars.append(char)
    ok=0


    for char in chars:
        if temp_chars.count(char) >= 1:
            ok += 1


    if ok != len(word):
        return False
        exit()
    else:
        if len(target) == len(word) +1:
            print("For word", target)
            return True
            exit()
        else:
            return False
            exit()





def game():
    outcome = ""
    fp = open("wordsEn.txt")
    contents = fp.read()
    words = contents.split(" ")

    user_input = input("Give num from 1 to 15")
    num = int(user_input)

    if outcome ==  "" :
        my_choice = choice(list(alphabet))
        outcome += my_choice + "\n"
        line = my_choice



    for word in words:
        if len(line) > num:
            return outcome
            exit()
        else:
            if finding(line,word):
                line = word
                outcome += line + "\n" 


print(game())

第一个“查找”功能起作用了,它会找到下一个单词来填充金字塔的下一行。我已经尝试了几个单词的小列表的代码,虽然结果字符串(这是一个存储金字塔的字符串)具有正确的形式,但是在小示例和包含所有英文单词的大例子。

【问题讨论】:

  • 你能分享文件“wordsEn.txt”吗? (如果它不大,例如在 pastebin 上)
  • 我提到的文件在一个平台内,我不知道如何访问它,但这里github.com/dwyl/english-words 它实际上是相同的单词列表。这会有帮助吗?
  • 欢迎来到 StackOverflow。见minimal, reproducible example。在您发布 MRE 代码并准确说明问题之前,我们无法有效地帮助您。我们应该能够将您发布的代码粘贴到文本文件中并重现您指定的问题。其中一部分包括您的单词列表。不,我们不需要指向完整词典的外部链接 - 只需几个词即可说明您的代码存在的问题。
  • 我不确定我是否理解出了什么问题。你在某处得到None
  • 忘了补充我同意@Prune,重要的是我们可以理解数据的格式,我们不一定需要整个列表。

标签: python python-3.x string list dictionary


【解决方案1】:

虽然我们正在等待有关输入数据格式的更多详细信息,但我想我会想出一个基本的解决方案,使用来自 github.com/dwyl/english-words 的 words_alpha.txt 字样。

我可能会制作几个不同的版本,我会看看有没有明显优于其他版本的。

import collections as colls
import operator as op
import random

words_dict = colls.defaultdict(list)

with open('../resources/words_alpha.txt') as words_file:
    for line in words_file:
        line_clean = line.strip()
        words_dict[len(line_clean)].append(line_clean)

# Python 3.7+, dictionaries are in insertion order.
words_list = list(words_dict.values())

pyramid_len = int(input("Enter the size of the pyramid: "))

for curr_words in words_list[:pyramid_len]:
    print(random.choice(curr_words))

【讨论】:

    猜你喜欢
    • 2013-08-13
    • 1970-01-01
    • 2021-04-17
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 2022-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多