首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将值保存到列表中,并将其发送给函数。

将值保存到列表中,并将其发送给函数。
EN

Stack Overflow用户
提问于 2018-12-13 23:40:12
回答 1查看 81关注 0票数 1

因此,我试图创建一个脚本,在其中循环遍历文本文件,以便能够保存txt文件中的所有值,然后将其发送到函数中。我将在下面的代码之后解释它:

代码语言:javascript
复制
randomnames.txt

Alejandro  
Tisha  
Eleni  
Milton  
Jeanice  
Billye  
Vicki  
Shelba  
Valorie  
Penelope  
Mellissa  
Ambrose  
Retta  
Milissa  
Charline  
Brittny  
Ehtel  
Hilton  
Hobert  
Lakendra  
Silva  
Lawana  
Sidney  
Janeen  
Audrea  
Orpha  
Peggy  
Kay  
Marvis  
Tia  
Randy  
Cary  
Santana  
Roma  
Mandi  
Tyrone  
Felix  
Maybelle  
Leonia  
Micha  
Idalia  
Aleida  
Elfrieda  
Velia  
Cassondra  
Drucilla  
Oren  
Kristina  
Madison  
Dia  


names.txt

Alejandro
Tisha
Eleni
Dia
Hobert
代码语言:javascript
复制
import json, time, sys, os, timeit, random, colorama, requests, traceback, multiprocessing, re
from random import choice
import threading


def get_names():

    name_test = [line.rstrip('\n') for line in open('randomnames.txt')]
    return name_test

def filter(thread, i):

    text = thread

    positive_keywords = [i]

    has_good = False

    for ch in ['&', '#', '“', '”', '"', '*', '`', '*', '’', '-']:
        if ch in text:
            text = text.replace(ch, "")

    sentences = [text]

    def check_all(sentence, ws):
        return all(re.search(r'\b{}\b'.format(w), sentence) for w in ws)

    for sentence in sentences:
        if any(check_all(sentence, word.split('+')) for word in positive_keywords):
            has_good = True
            break

    if not has_good or i == "":
        sys.exit()

    print('Matched ' + text)

def main():
    old_list = []

    old_names_list = []

    while True:

        new_names_list = [line.rstrip('\n') for line in open('names.txt')]
        for new_thread in get_names():

            if not new_names_list == old_names_list:
                for i in new_names_list:
                    if not i in old_names_list:
                        threading.Thread(target=filter, args=(new_thread, i)).start()
                        if new_thread not in old_list:
                            old_list.append(new_thread)

            elif new_thread not in old_list:
                threading.Thread(target=filter, args=(new_thread, new_names_list)).start()
                old_list.append(new_thread)

        else:
            randomtime = random.randint(1, 3)
            print('No changes!')
            time.sleep(randomtime)

        old_names_list = new_names_list
if __name__ == '__main__':
    try:
        main()

    except KeyboardInterrupt:
        print('Keyboard - Interrupted' )
        sys.exit()

程序现在的工作方式是检查randomnames.txt中的所有名称,并检查其中任何一个名称是否与names.txt中的名称相匹配。如果有匹配,它将打印出一个匹配,如果没有,则只执行sys.exit (这会杀死线程)。

然而,我的问题在于

代码语言:javascript
复制
if not new_names_list == old_names_list:
                    for i in new_names_list:
                        if not i in old_names_list:
                            threading.Thread(target=filter, args=(new_thread, i)).start()
                            if new_thread not in old_list:
                                old_list.append(new_thread)

                elif new_thread not in old_list:
                    threading.Thread(target=filter, args=(new_thread, new_names_list)).start()
                    old_list.append(new_thread)

我认为问题在于它在哪里运行大量线程,因为它从names.txt获取一个名称,并在randomnames.txt中逐个检查所有名称(线程)。这意味着,如果randomnames.txt中有50个名称,它将创建50个线程,检查来自randomnames.txt的任何名称是否与来自names.txt的名称相匹配。如果它匹配,那么它将打印出一个匹配。问题是,它需要创建50个线程,只有一个名称,这意味着它将添加另外50个线程,以跟踪新的名称。

我认为这是一个问题,以及如何解决这个问题的原因是,如果可能将来自names.txt的所有名称添加到一个列表中,然后将其发送到filter(),那么它将检查names.txt中是否有与randomnames.txt中的名称匹配的名称。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-12-14 06:16:50

原帖中的代码过于复杂。从根本上说,您是在比较匹配的两个名称集合。这是集合论,您应该使用Python集来完成这一任务,而不是使用列表。考虑:

代码语言:javascript
复制
names = {'Bob', 'Cindy', 'Dave'}
other_names = {'Lou', 'Pete', 'Cindy'}
print(names & other_names)  # {‘Cindy’}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53771632

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档