【问题标题】:Python string comparison whilst in loop循环中的Python字符串比较
【发布时间】:2013-06-16 13:52:18
【问题描述】:

我对这个论坛、编程和 Python 都很陌生。我正在尝试开发我的第一个程序,但是在一个特定问题上我一直遇到困难。我很高兴某种鞋底可以让我摆脱痛苦并告诉我如何正确地做我想做的事。如果您知道自己在做什么,我敢肯定这很简单,但是目前我很愚蠢并且不知道自己在做什么:-)

例子:

我需要处理 2 个文件,A 和 B

文件 A 包含以下文本:

This is a test

虽然文件 B 包含文本:

h
t
s
i
a

我需要创建一个程序,从文件 A 中一次抓取 1 个字符,然后在文件 B 中搜索相同的字符。一旦程序找到匹配项,我希望它打印找到匹配项的行号,然后继续从文件 A 中抓取另一个字符并重复此过程直到 EOF。

【问题讨论】:

  • 嗨,欢迎来到 StackOverflow!您选择了一个有趣的挑战(恭喜!)并且您已经很好地描述了它。但是,您根本没有说您卡在哪里 - 读取文件,迭代它,获取子字符串,比较它们......您能展示一下您到目前为止所写的内容吗?这样我们就可以更轻松地为您提供具体建议。

标签: python string loops comparison match


【解决方案1】:

好的,让我们一步一步来。首先,我会将文件B 读入一个非常适合快速查找的结构,因为我们将经常这样做:

chars = {}
with open("B") as lookupfile:
    for number,line in enumerate(lookupfile):
        chars[line.strip()] = number

现在我们有一个字典chars,其中包含作为键的字母和作为值的行号:

>>> chars
{'t': 1, 'a': 4, 'i': 3, 'h': 0, 's': 2}

现在我们可以遍历第一个文件。文件的标准 Python 迭代器每次迭代消耗一个 line,而不是一个 character,因此最好将整个文件读入一个字符串,然后对其进行迭代(因为对于字符串,迭代是逐个字符的):

with open("A") as textfile:
    text = textfile.read()

现在我们遍历字符串并打印匹配值:

for char in text:
    if char in chars:
        print("Character {0} found in row {1}".format(char, chars[char]))

如果你不喜欢两次访问字典,你也可以使用

for char in text:
    found = chars.get(char):    # returns None if char isn't a key in chars
    if found:
        print("Character {0} found in row {1}".format(char, found))

或者,使用异常:

for char in text:
    try:
        print("Character {0} found in row {1}".format(char, chars[char]))
    except KeyError:
        pass

【讨论】:

  • 嗨,蒂姆,非常感谢您在这方面的帮助。试用了您的代码,它运行良好。我现在只需要修改以适应我自己的小程序。我还将花一些时间研究您的代码,以便将来从中学习。再次感谢!克林顿。
【解决方案2】:
import os
fA = open('C:\\Desktop\\fileA.txt', 'r')
fB = open('C:\\Desktop\\fileB.txt', 'r')

fileb_content = []
for line in fB:
    fileb_content.append(fB.read().split('\n'))

rA = fA.readline().split('\n')[0]

for c in list(rA):
        if(c.strip()):
            if(c.lower() in fileb_content[0]):
                print(fileb_content[0].index(c.lower()))

这里我测试那个字符不为空。

【讨论】:

  • 您好,反垄断,感谢您的投入和时间。在尝试了另一个好心回答的人的代码后,现在所有这些都排序了。克林顿。
【解决方案3】:

首先读取文件A并将其内容存储在一个变量中(使用file.read)。

with open('A.txt') as f:

    data = f.read()  # now data is: "This is a test"
    # now data is string that dontains all data of the file A.
    # But as searching a character in a string is an O(N) operation
    # so we must convert this string to a better data-structure.
    # As we need the item as well as their first index so we
    # should create a dict here, with character as the key and
    # it's first appearance(first index) as it's value. 
    # Dicts provide O(1) lookup.

    dic = {}
    for ind, char in enumerate(data):
        # store the character in dict only if it is an alphabet
        # also check if it's already present is dict or not.
        if char.isalpha() and char not in dic:
            dic[char] = ind
    #dic is {'a': 8, 'e': 11, 'i': 2, 'h': 1, 's': 3, 'T': 0, 't': 10}

现在打开文件 B 并使用 for 循环对其进行迭代,文件迭代器上的 for 循环一次返回一行。(内存高效方法)。

with open('B.txt') as f:
    for char in f:            #iterate one line at a time 
        char = char.strip()   #str.strip strips off whitespaces like '\n'
        if char in dic:
           print dic[char]     # if character is found in dict then
                              # print it's value, i.e index
...             
1
10
3
2
8

【讨论】:

  • 嗨 Ashwini,感谢您的帮助。我试过你的选择,也是蒂姆下面的选择。不幸的是,当我尝试您的代码时,出现了一些错误并且没有真正发生。因此,我没有尝试找出问题所在,而是尝试了 Tim 的代码,该代码运行良好。不过感谢您的帮助!我真的很感激:-)
  • @user2490853 到底有什么错误?我修正了一个错字,现在它应该可以正常工作了。
猜你喜欢
  • 2017-08-13
  • 1970-01-01
  • 2018-03-27
  • 1970-01-01
  • 1970-01-01
  • 2013-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多