Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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_Search_List_Performance - Fatal编程技术网

在大型列表中查找/搜索的最有效方式(python)

在大型列表中查找/搜索的最有效方式(python),python,search,list,performance,Python,Search,List,Performance,--我刚刚解析了一个大文件,创建了一个包含42000个字符串/单词的列表。我想查询[这个列表]来检查给定的单词/字符串是否属于它。所以我的问题是: 进行这种查找最有效的方法是什么 第一种方法是对列表进行排序(list.sort()),然后使用 >> if word in list: print 'word' 这真的很琐碎,我相信有更好的方法。我的目标是应用快速查找,查找给定字符串是否在此列表中。如果您对其他数据结构有任何想法,欢迎使用。但是,我现在想避免使用更复杂的数据结构,如tr

--我刚刚解析了一个大文件,创建了一个包含42000个字符串/单词的列表。我想查询[这个列表]来检查给定的单词/字符串是否属于它。所以我的问题是:

进行这种查找最有效的方法是什么

第一种方法是对列表进行排序(
list.sort()
),然后使用

>> if word in list: print 'word'
这真的很琐碎,我相信有更好的方法。我的目标是应用快速查找,查找给定字符串是否在此列表中。如果您对其他数据结构有任何想法,欢迎使用。但是,我现在想避免使用更复杂的数据结构,如trytes等。我感兴趣的是听一些关于快速查找或任何其他python库方法的想法(或技巧),这些方法可能比中的简单
更快地进行搜索


我还想知道搜索项的索引

不要创建
列表
,创建
集合
。它在固定时间内进行查找

如果您不想占用集合的内存开销,请保留一个已排序的列表,并使用模块对其进行搜索

从对分导入左对分
def bi_包含(lst,项目):
“”“已排序列表的lst`中的'effective'项”“”
#如果项大于最后一个,则它不在列表中,但对分将
#找到'len(lst)`作为要插入的索引,所以首先检查它。否则,如果
#项目在列表中,则必须位于索引对分_左侧(lst,项目)

return(item关于集合与列表的一点未被考虑:在“解析大文件”中,人们希望需要处理重复的单词/字符串。您根本没有提到这一点


显然,向一个集合中添加新词会动态地删除重复项,而不需要额外的CPU时间或思考时间。如果你尝试使用一个列表,结果会是O(N**2)。如果你将所有内容都添加到一个列表中,并在最后删除重复项,最聪明的方法是……鼓点……使用一个集合,然后使用(小的)列表的内存优势很可能会被重复项淹没。

使用此程序,看起来dict是最快的,设置为第二位,带有bi_的列表包含第三位:

from datetime import datetime

def ReadWordList():
    """ Loop through each line in english.txt and add it to the list in uppercase.

    Returns:
    Returns array with all the words in english.txt.

    """
    l_words = []
    with open(r'c:\english.txt', 'r') as f_in:
        for line in f_in:
            line = line.strip().upper()
            l_words.append(line)

    return l_words

# Loop through each line in english.txt and add it to the l_words list in uppercase.
l_words = ReadWordList()
l_words = {key: None for key in l_words}
#l_words = set(l_words)
#l_words = tuple(l_words)

t1 = datetime.now()

for i in range(10000):
    #w = 'ZEBRA' in l_words
    w = bi_contains(l_words, 'ZEBRA')

t2 = datetime.now()
print('After: ' + str(t2 - t1))

# list = 41.025293 seconds
# dict = 0.001488 seconds
# set = 0.001499 seconds
# tuple = 38.975805 seconds
# list with bi_contains = 0.014000 seconds

非常感谢您的详细回复。实际上我自己也在考虑应用二进制搜索,但正如我所看到的,对分模块就是这样做的,所以您节省了我的时间:)。再次感谢您的帮助。@user229269,您锁定到了文章的错误部分!你可能想要一个
集合
,而不是一个
列表
。@Mike Graham我知道你在说什么,但我担心如果我使用集合,我可能会遇到内存问题,因为我的列表实际上是一个快速增长的单词列表,最终会有10万个字符串和more@user229269,100000件物品并不多。使用
集合
而不是
列表
,那么多项目只会增加内存使用量是的,事实上你(@Mike Graham)是对的:)--我已经使用集合了。非常感谢你让我重新考虑它,因为dicts的速度更快。下一个问题是生成“l_单词”对象需要多长时间+1.如果您预期稍后会进行复杂的查找—我所说的复杂并非微不足道—我建议您将其存储在
sqlite3
中。
from datetime import datetime

def ReadWordList():
    """ Loop through each line in english.txt and add it to the list in uppercase.

    Returns:
    Returns array with all the words in english.txt.

    """
    l_words = []
    with open(r'c:\english.txt', 'r') as f_in:
        for line in f_in:
            line = line.strip().upper()
            l_words.append(line)

    return l_words

# Loop through each line in english.txt and add it to the l_words list in uppercase.
l_words = ReadWordList()
l_words = {key: None for key in l_words}
#l_words = set(l_words)
#l_words = tuple(l_words)

t1 = datetime.now()

for i in range(10000):
    #w = 'ZEBRA' in l_words
    w = bi_contains(l_words, 'ZEBRA')

t2 = datetime.now()
print('After: ' + str(t2 - t1))

# list = 41.025293 seconds
# dict = 0.001488 seconds
# set = 0.001499 seconds
# tuple = 38.975805 seconds
# list with bi_contains = 0.014000 seconds