创建矩阵

时间:2011-09-13 10:11:25

标签: python class dictionary vector matrix

我希望我的程序能够输入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 个答案:

答案 0 :(得分:1)

不要以为我得到了这个问题。 然而,这里有一些片段可以帮助您入门。

只需一次阅读25个字母,然后按照here所述使用分区生成器 使用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]]

在此之后我想你需要澄清一下你的问题: - )