在字典中搜索更多数据

search the dictionary for more data

我创建了这个脚本,我在这里尝试了各种方法,但没有任何帮助,所以我想作为更有经验的人与您联系

基本上脚本原理是做什么的?

我放在这里的代码就是这样做的

输入

1:1:1

输出

1:1:1 this is text from file

效果很好,但如果我想搜索两个结果,所以它在这里不起作用,我尝试了几种方法

问题是我想同时找两个东西

输入1:1:1 === 4:1:2===0---0 我在这里拆分一下

split ("===") [0]
split ("===") [1]
split ("===") [2]

我正在寻找的输出是

1: 1: 1 this is text from file 4: 1: 2 this is next text resolut 0 --- 0

从中获取数据的文件看起来像这样

"numberssearch.txt"

1:1:1===4:1:2===0---0
1:1:1===4:2:3===-1----1
1:1:1===4:3:4===-2----2
1:1:1===4:4:5===-3----3
1:1:1===4:5:6===-4----4
1:1:1===4:6:7===-5----5

这就是它从中获取数据的文件的样子

"book.txt"

1:1:1   text1
1:1:2   text2
1:1:3   text3
1:1:4   text4
1:1:5   text5
1:1:6   text6
1:1:7   text7
ect... 

这是代码

with open ("numberssearch.txt") as f:
         unsolved = set (f.read (). split ())
for unsolved_elem in unsolved:
    userinput = unsolved_elem
    file =  open("book.txt",encoding='ISO-8859-1')
    lines = file.readlines()
    for line in lines:
        words = line.split()
        unsolved_elem = unsolved_elem.split("===")[0]
        if len(words) > 0 and unsolved_elem == words[0]:
           print(line, file=open(""+str("solved")+".txt", "a",encoding='ISO-8859-1'))

它需要 运行 两个嵌套循环 - 并且它需要在两个循环中从文件 book 中读取 - 所以最好先读取数据 book.txt 和创建字典。


我使用 io 只是为了模拟内存中的文件 - 所以每个人都可以简单地复制并 运行 它。
但是你应该使用 open()

在这个版本中,我假设 book 可能有很多次相同的数字,所以它需要列表来保留这个数字的所有文本。

book_txt = '''1:1:1   text1
1:1:2   text2
1:1:3   text3
1:1:4   text4
1:1:5   text5
1:1:6   text6
1:1:7   text7
4:2:3   other3
4:2:4   other4
4:4:5   other5'''

import io

# --- read book.txt ---

book = {}

#with open("book.txt") as f:
with io.StringIO(book_txt) as f:
    for line in f:
        number, text = line.strip().split(" ", 1)
        if number not in book:
            book[number] = []
        book[number].append(text)
        
for item in book.items():
    print(item)

结果:

('1:1:1', ['text1'])
('1:1:2', ['text2'])
('1:1:3', ['text3'])
('1:1:4', ['text4'])
('1:1:5', ['text5'])
('1:1:6', ['text6'])
('1:1:7', ['text7'])
('4:2:3', ['other3'])
('4:2:4', ['other4'])
('4:4:5', ['other5'])

与文件 numberssearch.txt 类似 - 首先我会阅读所有行和 运行 split('===')

numberssearch_txt = '''1:1:1===4:1:2===0---0
1:1:1===4:2:3===-1----1
1:1:1===4:3:4===-2----2
1:1:1===4:4:5===-3----3
1:1:1===4:5:6===-4----4
1:1:1===4:6:7===-5----5'''

import io

# --- read numberssearch.txt ---

unsolved = set()

#with open ("numberssearch.txt") as f:
with io.StringIO(numberssearch_txt) as f:
    for line in f:
        parts = tuple(line.strip().split('==='))
        unsolved.add(parts)

for item in unsolved:
    print(item)

结果:

('1:1:1', '4:5:6', '-4----4')
('1:1:1', '4:4:5', '-3----3')
('1:1:1', '4:1:2', '0---0')
('1:1:1', '4:3:4', '-2----2')
('1:1:1', '4:6:7', '-5----5')
('1:1:1', '4:2:3', '-1----1')

然后我会在 unsolved

中搜索 book 中的数字
for number1, number2, rest in unsolved:

    if (number1 in book) and (number2 in book):

        # nested loops which use `book`
        for text1 in book[number1]:
            for text2 in book[number2]:
                print(number1, text1, number2, text2, rest)

结果:

1:1:1 text1 4:4:5 other5 -3----3
1:1:1 text1 4:2:3 other3 -1----1

完整的工作代码:

编辑: 我根据评论添加了更改。

# files with empty lines
book_txt = '''1:1:1   text1
1:1:2   text2
1:1:3   text3

1:1:4   text4
1:1:5   text5
1:1:6   text6
1:1:7   text7

4:2:3   Hello World 1
4:2:4   Hello World 2
4:4:5   Hello World 3'''

numberssearch_txt = '''1:1:1===4:1:2===0---0
1:1:1===4:2:3===-1----1

1:1:1===4:3:4===-2----2
1:1:1===4:4:5===-3----3

1:1:1===4:5:6===-4----4
1:1:1===4:6:7===-5----5'''

import io

# --- read book.txt ---

book = {}

#with open("book.txt") as f:
with io.StringIO(book_txt) as f:
    for line in f:
        line = line.strip()
        if line:
            parts = line.split(" ", 1)
            if len(parts) == 2:            
                number, text = parts
                text = text.strip()
                if number not in book:
                    book[number] = []
                book[number].append(text)

for item in book.items():
    print(item)

# --- read numberssearch.txt ---

unsolved = set()

#with open ("numberssearch.txt") as f:
with io.StringIO(numberssearch_txt) as f:
    for line in f:
        line = line.strip()
        if line:
            parts = tuple(line.split('==='))
            if len(parts) == 3:
                unsolved.add(parts)

for item in unsolved:
    print(item)

# --- search ---

for number1, number2, rest in unsolved:

    if (number1 in book) and (number2 in book):

        for text1 in book[number1]:
            for text2 in book[number2]:
                print(number1, text1, number2, text2, rest)

如果book.txt可能每个数字只有一次那么代码会更简单。

编辑: 我根据评论添加了更改。

# files with empty lines
book_txt = '''1:1:1   text1
1:1:2   text2
1:1:3   text3

1:1:4   text4
1:1:5   text5
1:1:6   text6
1:1:7   text7

4:2:3   Hello World 1
4:2:4   Hello World 2
4:4:5   Hello World 3'''

numberssearch_txt = '''1:1:1===4:1:2===0---0
1:1:1===4:2:3===-1----1

1:1:1===4:3:4===-2----2
1:1:1===4:4:5===-3----3

1:1:1===4:5:6===-4----4
1:1:1===4:6:7===-5----5'''

import io

# --- read book.txt ---

book = {}

#with open("book.txt") as f:
with io.StringIO(book_txt) as f:
    for line in f:
        line = line.strip()
        if line:
            parts = line.split(" ", 1)
            if len(parts) == 2:            
                number, text = parts
                text = text.strip()
                book[number] = text

for item in book.items():
    print(item)

# --- read numberssearch.txt ---

unsolved = set()

#with open ("numberssearch.txt") as f:
with io.StringIO(numberssearch_txt) as f:
    for line in f:
        line = line.strip()
        if line:
            parts = tuple(line.split('==='))
            if len(parts) == 3:
                unsolved.add(parts)

for item in unsolved:
    print(item)

# --- search ---

for number1, number2, rest in unsolved:
    if (number1 in book) and (number2 in book):
        text1 = book[number1]
        text2 = book[number2]
        print(number1, text1, number2, text2, rest)