0
my_str="ellezagchickenbndodetballigatoraaaolmeznacattleeblrctacfenarcesssadlritfhftrarrssos aoiarefaareppohssarghcerumrheirmmwildfcumeboimltaairfovindalbiglalobehoeasiaxuesabldinbbccrhhrtylrlahsifdlogkrctlaiareogoldfinchefnnddmneepoletnarntadodinosauroxofoeclictnahpelepalgaierhohcaorkcocyatrmoacrflamingoerefafloechateehchdracaribou"
def create_2d_list(N):
    output_list=[]
    counter=0
    for row in range(0,N):
        temp=[]
        for col in range(0,N):
            temp.append(my_str[counter])#you can add a charcter instead of counter
            counter=counter+1
        output_list.append(temp[:])
    return output_list

N=18

x=create_2d_list(N)

for row in range(0,N):
    total=0
    s="|"
    for col in range(0,N):
        my_str="{0:2} ".format(x[row][col])
        s=s+my_str+"|"
    print "-"*(N*4+1)
    print s,
    print " "

the_valid_words=open("E:/asd/words.txt","r").readlines()


def looking_word_left_to_right(the_list):
    for any_words in the_valid_words:
        for every in x[0]:
            the_first_index=x[0].index(every)
            for every in range(the_first_index,(the_first_index)+7):
                c=str(every)
                the_join=" ".join(c)
                if the_join==the_valid_words:
                    word.upper().replace(every,x[0].upper(every))
                return x[0]

print looking_word_left_to_right(x)

每次我运行程序时,looking_word_left_to_right 都不会打印任何东西

PS它类似于初学者的小填字游戏,将组成一个单词的字母大写并删除所有其他字母而不改变位置,如果有人可以就如何继续进行类似的想法,那就太好了。我有一些有效的词要寻找。

我是一个新手,所以对我放轻松:) 感谢您的帮助。

4

1 回答 1

0

似乎有很多问题。

  • x当你也将它作为the_list"Just use传入时,你为什么还要进行操作the_list
  • 你只看第一线,x永远不会超越。
  • 看起来您在比较之前在每个字符之间放置了一个空格。如果 c = "abcdefg" 那么" ".join(c)会给你"a b c d e f g". 如果您的the_valid_words里面没有空格,那么if the_join==the_valid_words将始终评估为假。
  • 您正在比较the_valid_words,哪个是您的整个列表。您可能应该使用...与您在其他任何地方都没有使用的每个有效单词进行比较。any_word
  • 在更改它时,您可能还会遇到迭代问题x(它有时会使迭代器无效......但有时不会)。即,如果您正在迭代x,然后在完成迭代之前进行更改x,那么 Python 可能不知道迭代器在新版本的x. 既然和反正the_list一样x,最好还是先去做for every in the_list:,然后随心所欲x地改变。

看起来你可能不太明白 for 循环在 Python 中是如何工作的。看看那些,这可能会有所帮助。

于 2013-11-06T22:56:54.437 回答