Python优化:在字典或列表列表之间进行选择

时间:2017-09-20 21:02:00

标签: python python-3.x list dictionary

我是python3的新手,我对有关解决此问题的不同方法提出了疑问。关于使用不同数据结构的问题。 我的问题是如何比较权衡与不同的抽样技术

我在程序中使用字典数据结构来首先解决这个问题。然后我尝试仅使用列表数据结构重写它。我试着考虑排序的好处,我不知道这两种方法之间的区别是什么。它似乎没有在两种方法之间产生那么大的差异。

方法1.我使用字典在直方图中创建直方图键和值对

方法2以字符串格式接收源文本并返回列表列表 其中每个子列表中的第一个元素是单词,第二个元素是单词 元素是源文本中的频率

# This program Analyze word frequency in a histogram
# sample words according to their observed frequencies
# takes in a source text in string format and returns a dictionary
# in which each key is a unique word and its value is that word's
# frequency in the source text
import sys
import re
import random
import time

def histogram(source_text):
    histogram = {}
    # removing any sort of string, removing any other special character
    for word in source_text.split():
        word = re.sub('[.,:;!-[]?', '', word)

        if word in histogram:
            histogram[word] += 1
        else:
            histogram[word] = 1
    return histogram

def random_word(histogram):
    probability = 0
    rand_index = random.randint(1, sum(histogram.values()))
    # Algorithm 1
    for (key, value) in histogram.items():
        for num in range(1, value + 1):
            if probability == rand_index:
                if key in outcome_gram:
                    outcome_gram[key] += 1
                else:
                    outcome_gram[key] = 1
                # return outcome_gram
                return key
            else:
                probability += 1

#    Method 2 takes in a source text in string format and returns a list #of lists
#    in which the first element in each sublist is the word and the #second element is its frequency in the source texts

  # Algorithm 2
    # for word in histogram:
    #     probability += histogram[word]
    #     if probability >= rand_index:
    #         if word in outcome_gram:
    #             outcome_gram[word] += 1
    #         else:
    #             outcome_gram[word] = 1
            # return word


if __name__ == "__main__":
    outcome_gram = {}
    dict = open('./fish.txt', 'r')
    text = dict.read()
    dict.close()

    hist_dict = histogram(text)
    for number in range(1, 100000):
        random_word(hist_dict)

2 个答案:

答案 0 :(得分:5)

哪个更具可读性?我认为字典版本更容易理解。另请注意,您可以将第二个方法中的2元组列表传递给zshexpn(1)构造函数,以重现第一个方法的输出。这应该让您了解这两种实现如何至少在某些方面大致相同。除非这会导致性能问题,否则我不会太担心它。

Python的优势在于你可以用五行代码以可读的方式编写相同的代码。

dict

答案 1 :(得分:2)

我基本上同意Joran Beasley上面的评论,通常更好地解决你的问题,然后回去重构以提高效率。

使用直方图时,我建议您在集合模块中查看Counter。集合模块作为一个整体非常好,并且有很多有用的容器。

另一个很酷的模块是Timeit模块,它允许您在代码片段上运行小型计时实验。请记住,执行速度取决于许多不一定在程序控制范围内的因素。