Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.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 twitter位置)_Python_Algorithm_Twitter_Python Twitter - Fatal编程技术网

有没有更快的方法?(python twitter位置)

有没有更快的方法?(python twitter位置),python,algorithm,twitter,python-twitter,Python,Algorithm,Twitter,Python Twitter,我正试图返回一本字典,该字典按最近的州中心汇总推文。我迭代了所有的tweet,对于每个tweet,我检查所有的状态,看看哪个状态是最接近的 有什么更好的方法可以做到这一点 def group_tweets_by_state(tweets): """ The keys of the returned dictionary are state names, and the values are lists of tweets that appear closer to th

我正试图返回一本字典,该字典按最近的州中心汇总推文。我迭代了所有的tweet,对于每个tweet,我检查所有的状态,看看哪个状态是最接近的

有什么更好的方法可以做到这一点

def group_tweets_by_state(tweets):
    """

    The keys of the returned dictionary are state names, and the values are
    lists of tweets that appear closer to that state center than any other.

    tweets -- a sequence of tweet abstract data types """


    tweets_by_state = {}
    for tweet in tweets:
        position = tweet_location(tweet)
        min, result_state = 100000, 'CA'
        for state in us_states:
            if geo_distance(position, find_state_center(us_states[state]))< min:
                min = geo_distance(position, find_state_center(us_states[state]))
                result_state = state
        if result_state not in tweets_by_state:
            tweets_by_state[result_state]= []
            tweets_by_state[result_state].append(tweet)
        else:
            tweets_by_state[result_state].append(tweet)
    return tweets_by_state

在这个巨大的for循环中,每一个小小的增强都会在时间复杂度方面带来巨大的性能提升。当tweet的数量非常大时,我能想到的事情很少:

1.只需给geo_distance打一次电话,特别是在费用高昂的时候 现在find_state_center只被称为州数的50倍,而不是50*1000条推文,我们做了另一个巨大的改进

业绩总结
到1时,我们的性能翻了一番。2我们通过推特数量/位置次数来增强它。3是三者中最大的一个,与源代码相比,我们将时间复杂度降低到了tweet数量的1/1。

您的代码有效吗?那么你真的在乎它运行的时间吗?它这么慢吗?但现在,它非常慢。可能是因为你有很多推特要重复。在这种情况下,我同意最好优化时间。如果不是的话,可能是因为其他代码片段太慢了,运行起来需要几分钟。是的,我确实有很多推特要重复。哈哈。我懂了。但是有没有一种方法可以防止在每个状态上迭代呢?这是一个非常好的建议!谢谢保罗。@Steve我刚刚编辑了这篇文章,避免了对find_state_center不必要的访问,请欣赏:p4。通过构建kd树或其他一些州中心的空间分区数据结构,您可能会获得更高的性能。将每条推文检查的状态数从50个减少到9个左右。2:如果某种近似值是可接受的,则可以通过使用geohash来设置唯一坐标数的上限来改进。与其说有12000条推文和4000个独特的位置,不如说只有512条、256条、128条或64条独特的位置,这取决于你愿意牺牲多少准确性。
distance = geo_distance(position, find_state_center(us_states[state]))
if distance < min:
     min = distance
if geo_distance(position, find_state_center(us_states[state]))< min:
    min = geo_distance(position, find_state_center(us_states[state]))
position_closest_state = {}  # save known result 
tweets_by_state = {}
for tweet in tweets:
    position = tweet_location(tweet)
    min, result_state = 100000, 'CA'

    if position in position_closest_state:
        result_state = position_closest_state[position]
    else:
        for state in us_states:
            distance = geo_distance(position, find_state_center(us_states[state]))
            if distance < min:
                min = distance
                result_state = state
                position_closest_state[position] = result_state 
state_center_dict = {}
for state in us_states:
    state_center_dict[state] = find_state_center(us_states[state])

position_closest_state = {}  # save known result 
tweets_by_state = {}
for tweet in tweets:
    position = tweet_location(tweet)
    min, result_state = 100000, 'CA'

    if position in position_closest_state:
        result_state = position_closest_state[position]
    else:
        for state in us_states:
            distance = geo_distance(position, state_center_dict[state])
            if distance < min:
                min = distance
                result_state = state
                position_closest_state[position] = result_state