Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-从文本文件访问列表中的项目_Python_List_File - Fatal编程技术网

Python-从文本文件访问列表中的项目

Python-从文本文件访问列表中的项目,python,list,file,Python,List,File,我正在尝试对列表中的项目应用验证。我已经设法打开了它,但正在努力比较这两个词是否是拼字 这是我在终点站的结果 anagram: ['word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff'] Anagram anagram: ['word,word', 'stiff,schtiff', 'word,word',

我正在尝试对列表中的项目应用验证。我已经设法打开了它,但正在努力比较这两个词是否是拼字

这是我在终点站的结果

anagram:  ['word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff']

Anagram
anagram:  ['word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff']

anagram, not anagram, anagram, not anagram, anagram, not anagram, anagram, not anagram
在这个示例中,很明显我用两个相同的变量A,B做了错误的事情,但不确定该怎么做

word1 = open('a.txt', 'r').read().split()
word2 = open('a.txt', 'r').read().split()
count = {}
validation = True
if len(a) == len(b):
    for i in range(len(a)):
        if a[i] in count:
            count[a[i]] += 1
        else:
            count[a[i]] = 1  
        if b[i] in count:
            count[b[i]] += 1
        else:
            count[b[i]] = 1     
    for i in count:
        if count[i] % 2 == 0:
            validation = "Anagram"
        else:
            validation = "Not Anagram"
            break
else:
    validation = "Not Anagram"            
print(validation)
我到底在干什么

我想在终端实现这一点

anagram:  ['word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff']

Anagram
anagram:  ['word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff']

anagram, not anagram, anagram, not anagram, anagram, not anagram, anagram, not anagram
您可以尝试使用以下方法:

anagram = ['word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff', 'word,word', 'stiff,schtiff']
for elem in anagram:
    items = elem.split(",")
    firstLetters = set(items[0])
    secondLetters = set(items[1])
    if firstLetters == secondLetters:
        print("Anagram")
    else:
        print("Not anagram")
输出:

Anagram
Not anagram
Anagram
Not anagram
Anagram
Not anagram
Anagram
Not anagram
编辑:以下是如何从文件中读取并执行比较:

with open("anagram.txt","r") as inFile:
    words = [line for line in inFile]
    words = words[0].strip().split(",")
    first = []
    second = []
    for i in range(len(words)):
        if i%2 == 0:
            first.append(words[i])
        else:
            second.append(words[i])
    for f,s in zip(first,second):
        if set(f) == set(s):
            print("Anagram")
        else:
            print("Not anagram")

也许提供你想要达到的预期结果。完成。请参见上文^正在从文本文件读取数据集。这就是我发现的困难所在。那么如何获得
字谜的元素呢?如果您能为这两个文件提供一些示例数据,那么我就可以重现您的问题。我通过在文本编辑器中打开文件来检索它们。代码读取文本文件中的列表:a=open('anagram.txt','r')。read().split()对不起。我有一个单独的部分来打印列表:anagramlist=open('anagram.txt','r')。read().split()print(anagramlist)正如您所看到的,我复制了(我知道这是错误的),但不知道如何正确执行。