Creating a matrix
I want to my program to take 25 letters as input and put them in some sort of list so that i can make rules for what letter that can connect with another until and get three words from it that is not overlapping.
What i did was this:
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
So for example, when I run this it ask me for "One letter" * 25 which could generate a matrix looking something like this:
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']]
If anyone have a better way for doing this, I would be thankful if you shared it. And a way that will work with what I 开发者_如何学JAVAwant my program to do, which im not sure I will be able to with my version.
I have a dictionary.txt which I have made something like: dictionary = open('dictionary.txt', 'r')
so i thought that i would try to start matching matris[0][0]+matris[0][1]
and see if there is a word starting with i.e 'a'+'g'
, and then take the next letter and so on until you find the lets say three best (most valued) words.
I'm guessing that i would need a class. So this is how far I have come:
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
I thought that I have to save the positions in an array, so that I later on can make sure that the words that are best don't use the same letters?
Guessing I'm going to need some help with this class.
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}
I thought later on when I would give each letter a value, I would do it like this, but I dont know if that will be a good way?
Don't think I get the question. Here are however some snippets to get you started.
Just read 25 letters in one go and use a partitioning generator as described here
use split
to send it to the generator below.
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']]
If you are looking to do approximate matching, have a look at 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]]
After this I think you need to clarify your question a bit :-)
精彩评论