创建矩阵

0 投票
1 回答
582 浏览
提问于 2025-04-17 02:11

我想让我的程序接受25个字母作为输入,然后把它们放在一个列表里,这样我就可以制定一些规则,看看哪些字母可以连接在一起,最终得到三个不重叠的单词。

我做了以下这些:

def matris():
    matris = [[],[],[],[],[]]
    counter = 0
    counter2 = 0
    counter3 = 0
    counter4 = 0
    counter5 = 0

    while counter !=5:
        matris[0].append(raw_input('One letter: '))
        counter+=1

    while counter2 !=5:
        matris[1].append(raw_input('One letter: '))
        counter2+=1

    while counter3 !=5:
        matris[2].append(raw_input('One letter: '))
        counter3+=1

    while counter4 !=5:
        matris[2].append(raw_input('One letter: '))
        counter4+=1

    while counter5 !=5:
        matris[4].append(raw_input('One letter: '))
        counter5+=1

    return matris

举个例子,当我运行这个程序时,它会让我输入“一个字母” * 25,这样就能生成一个看起来像这样的矩阵:

matris = [['a', 'g', 'i', 't', 'u']
          ['s', 'r', 'g', 's', 'm']
          ['f', 'e', 'd', 'c', 't']
          ['r', 's', 'i', 'f', 'x']
          ['t', 'i', 't', 't', 'i']]

如果有人有更好的方法来实现这个功能,我会很感激你能分享一下。而且这个方法要能满足我想让程序完成的任务,我不太确定我现有的版本能否做到。

我有一个叫dictionary.txt的文件,我是这样打开它的: dictionary = open('dictionary.txt', 'r')

所以我想我可以尝试匹配 matris[0][0]+matris[0][1],看看是否有以比如说 'a'+'g' 开头的单词,然后继续拿下一个字母,直到找到三个最好的(最有价值的)单词。

我猜我需要一个类。所以这是我目前的进展:

Class hypotes:
  def __init__ (self, usedPosiiton, word):
  self.u = usedPosition
  self.w = word
  positions = []
  bestWords = [] # There should be maximum three words in this list, 
                 # the words with highest score

我想我需要把位置保存在一个数组里,这样我以后可以确保最好的单词不会使用相同的字母?

我想我会需要一些关于这个类的帮助。

letterValuePoints = {'A':50,  'B':110, 'C':190, 'D':70,  'E':50,  'F':90, 
                     'G':70,  'H':70,  'I':50,  'J':170, 'K':70,  'L':50,
                     'M':70,  'N':50,  'O':70,  'P':110, 'R':50,  'S':50, 
                     'T':50,  'U':110, 'V':90,  'X':190, 'Y':170, 'Z':210, 
                     'Å':110, 'Ä':90,  'Ö':110}

我想在给每个字母赋值的时候,我会这样做,但我不知道这样是否是个好方法?

1 个回答

1

我不太明白你的问题。不过,我可以给你一些代码片段,帮助你入门。

你可以一次性读取25个字母,然后使用一个分区生成器,具体的描述可以在这里找到。接着,使用split把它发送到下面的生成器。

def chunks(l, n):
    """ Yield successive n-sized chunks from l.
    """
    for i in xrange(0, len(l), n):
        yield l[i:i+n]

import pprint
a=raw_input('some letters: ')
some letters: 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

pprint.pprint(list(chunks(a.split(), 5)))
[['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']]

如果你想做大致匹配,可以看看difflib

>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']
>>> import keyword
>>> get_close_matches('wheel', keyword.kwlist)
['while']
>>> get_close_matches('apple', keyword.kwlist)
[]
>>> get_close_matches('accept', keyword.kwlist)
['except']

[[0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
 [10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
 [20, 21, 22, 23, 24, 25, 26, 27, 28, 29],
 [30, 31, 32, 33, 34, 35, 36, 37, 38, 39],
 [40, 41, 42, 43, 44, 45, 46, 47, 48, 49],
 [50, 51, 52, 53, 54, 55, 56, 57, 58, 59],
 [60, 61, 62, 63, 64, 65, 66, 67, 68, 69],
 [70, 71, 72, 73, 74]]

在这之后,我觉得你需要稍微澄清一下你的问题 :-)

撰写回答