如何比较数组的两个索引(不是这些索引的值)?

我需要制作一个程序来定义这个词是否是字谜。嗯,这是我的代码。标有 的线条## here反映了主要问题。我需要定义的索引word1和word2第一和第二的for循环,然后对它们进行比较。如果这些索引相似,那么程序就会错过这个词(因为它在迭代时出现了两次)。我知道,代码还没有完成


text_sample = 'text goes here'


c = 0

i = 0

isanagramma = 0

n = len(text_sample)

while c < len(text_sample):

    for word1 in text_sample:

        for word2 in range(1,n):

            s1 = sorted(word1)

            s2 = sorted(text_sample[word2])

            index1 = text_sample.index(word1)                ## here

            index2 = text_sample.index(text_sample[word2])   ## here

            if index1 == index2:                             ## here

                continue                                     ## here

            if s1 == s2:

                isanagrama+=1

            i+=1

    c+=1

print(isanagramma)


茅侃侃
浏览 132回答 1
1回答

沧海一幻觉

text_sample = 'text goes here ttex's = text_sample.split(" ")isanagramma = 0for i in range (0,len(s)-1):&nbsp; &nbsp; for x in range(i+1,len(s)):&nbsp; &nbsp; &nbsp; &nbsp; if sorted(s[i]) == sorted(s[x]):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ("is anagram :{} and {}".format(s[i],s[x]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; isanagramma += isanagramma&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print ("not anagram :{} and {}".format(s[i],s[x]))print (isanagramma)输出:not anagram :text and goesnot anagram :text and hereis anagram :text and ttexnot anagram :goes and herenot anagram :goes and ttexnot anagram :here and ttex1
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python