【问题标题】:Find char index in list在列表中查找字符索引
【发布时间】:2021-01-05 13:44:17
【问题描述】:

我想在 .txt 文件中搜索某个字符 (X),如果存在,请执行某个操作。不幸的是,这段代码不起作用:

file = "test.txt"
file = open(file, "r") 
lines = file.readlines()


# char in the list
if "X" in lines:
    print('X is present in the list')

# char not in the list
if "X" not in lines:
    print('X is not present in the list')

test.txt 文件:

XX X XXX X XX
XXXXX XX X    XX

有什么想法吗?

P.S.:即使将“X”更改为“X”也不起作用。

【问题讨论】:

  • if "X" not in Zeilen: 中的变量Zeilen 是什么?
  • lines 是一个字符串列表。没有一行与字符串 "X" 完全匹配。
  • How to debug small programs。逐步检查您的代码并查看 lines 包含的内容。 'X' in lines 会是 True 吗?在 Python 中,"X"'X' 也意味着同样的事情。
  • "x" in lines 替换为if any("X" in words for words in lines):。它将在列表中的字符串中查找 x
  • Zeilen 只是翻译错误,抱歉。我刚刚用英语为你重命名了变量

标签: python list char


【解决方案1】:

如果您需要知道X 出现的行号:

target = 'X'
counter = 0
with open('testing.txt') as f:
    for i, line in enumerate(f):
        if target in line:
            counter += 1
            print('target is present in line {}'.format(
                i+1))

print('target appears in {} lines'.format(counter))

如果您还需要知道X 出现的列号:

target = 'X'
counter = 0
with open('testing.txt') as f:
    for i, line in enumerate(f):
        for j, char in enumerate(line):
            if target == char:
                counter += 1
                print('target is present in line {} at column {}'.format(
                    i+1, j+1))

print('target appears {} times'.format(counter))

一些澄清:

  • with ... open 完成后会自动关闭文件,因此您无需记住显式关闭它
  • for i, line in enumerate(f): 逐行迭代,而不是一次将它们全部加载到内存中。

【讨论】:

    【解决方案2】:

    由于readlines() 是一个列表,您需要迭代并签入行。也许您可以为此使用for else

    file = "testing.txt"
    file = open(file, "r") 
    lines = file.readlines()
    
    # char in the list
    for line in lines:
        if "X" in line:
            print('X is present in the list')
            break
    else:
        print('X is not present in the list')
    

    它遍历每一行,如果任何行有字符break,则调用else,只有在任何行中都找不到字符时才会运行。

    更新

    如果你想计数,那么你可以简单地在循环中增加计数器,一旦循环完成,检查计数器:

    file = "testing.txt"
    file = open(file, "r") 
    lines = file.readlines()
    
    # char in the list
    counter = 0
    for line in lines:
        if "X" in line:
            counter += 1  # < -- count
            
    if counter: # check    
        print('X is present in the list')
            
    else:
        print('X is not present in the list') 
    

    【讨论】:

    • 好的,该程序可以运行,但我如何才能检查列表中出现“X”的频率?这不起作用: `````` file= "test.txt" file= open(file, "r") lines= file.readlines() counter = 0 for line in lines: if "X" in line:计数器 = 计数器 + 1 打印(计数器)````
    【解决方案3】:

    正如你所问的,我将输出一个带有项目Yes, there are 3 X's 的列表。

    我有我的dataf.txt 文件:

    XXX X XX X XX
    X ASD X F FFFF XX
    D X XXX X EFX
    A F G
    

    将数据读入文件并计算 X 的数量(注意:我使用了列表理解技巧!)

    with open('dataf.txt','r') as fl:
        data = fl.readlines()
        a = ['Yes, there are '+str(i.count('X')) + " X's" if 'X' in i else "No X's" for i in data]
        print(a)
    

    输出:

    ["Yes, there are 9 X's", "Yes, there are 4 X's", "Yes, there are 6 X's", "No X's"]
    

    【讨论】:

    • 列表推导是非常推荐的创建列表的方法,而不是 hack
    • @PranavHosangadi 是的,但我喜欢称之为 hack :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-17
    • 2019-04-29
    • 2013-05-08
    • 1970-01-01
    • 2013-07-28
    • 2021-12-24
    • 1970-01-01
    相关资源
    最近更新 更多