Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/286.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_Performance_List_Dictionary_Append - Fatal编程技术网

Python 检查字典中的值

Python 检查字典中的值,python,performance,list,dictionary,append,Python,Performance,List,Dictionary,Append,我对如何解决这个问题有一个想法,但我需要用大量数据对其进行优化。 我非常希望找到每个状态中的所有值,并保留它们出现次数的计数器 courses = {} def insertIntoDataStruct(state,job,count,dict): if not state in dict: #adds state to list with first job and count dict[state] = [[job,count]] else:

我对如何解决这个问题有一个想法,但我需要用大量数据对其进行优化。 我非常希望找到每个状态中的所有值,并保留它们出现次数的计数器

courses = {}

def insertIntoDataStruct(state,job,count,dict):
    if not state in dict: #adds state to list with first job and count
        dict[state] = [[job,count]]
    else:
        dict[state].append([job,count])

insertIntoDataStruct("TX", 214, 1, courses)
insertIntoDataStruct("CA", 3124, 1, courses)
insertIntoDataStruct("TX", 21455, 1, courses)
insertIntoDataStruct("CA", 5124, 1, courses)
insertIntoDataStruct("CA", 5124, 1, courses)
这将产生:

{'CA': [[3124, 1], [5124, 1], [5124, 1]], 'TX': [[214, 1], [21455, 1]]}
因此,如果在CA中,值5124被添加两次,则应改为输出:

{'CA': [[3124, 1], [5124, 2], 'TX': [[214, 1], [21455, 1]]}
如果数字已经在状态中,我可以创建一个for循环来检查每次追加的内容,但是当我在添加每一行时得到数千个要检查的值时,它会占用大量的时间

优化这个的最佳方法是什么?

我会将它构造为一个对象集。这两个字典子类将组成查找状态和作业
O(1)
,这意味着您不必手动检查
struct
struct[state]
中的键-您只需添加
计数,就好像它已经存在一样

>>> from collections import Counter, defaultdict
>>> def insert_into_data_struct(state, job, count, struct):
    struct[state][job] += count


>>> courses = defaultdict(Counter)
>>> insert_into_data_struct("TX", 214, 1, courses)
>>> insert_into_data_struct("CA", 3124, 1, courses)
>>> insert_into_data_struct("TX", 21455, 1, courses)
>>> insert_into_data_struct("CA", 5124, 1, courses)
>>> insert_into_data_struct("CA", 5124, 1, courses)
>>> courses
defaultdict(<class 'collections.Counter'>, {'CA': Counter({5124: 2, 3124: 1}), 
                                            'TX': Counter({214: 1, 21455: 1})})

要对普通字典做同样的事情(
courses={}
):


不过,你现在必须自己找到前三名

如何使用此方法仅打印每个州最高的集合?
Counter
有一个
most_common(n)
方法,因此您可以使用
{state:count.most_common(3)for state,count in courses.items()}
获得每个州最高的三个集合。到目前为止,我已经完成了此count=Counter(courses)打印计数但当我尝试打印并冻结我的应用程序时,它会被锁定。为什么您要创建一个
课程的
计数器
?当时,
courses
是否已经是一个
defaultdict
?因此我刚刚发现shell必须在运行2.4时运行我的代码,所以计数器和defaultdict将无法工作,返回到寻找替代解决方案:(
>>> {state: count.most_common(3) for state, count in courses.items()}
{'CA': [(5124, 2), (3124, 1)], 'TX': [(214, 1), (21455, 1)]}
def insert_into_data_struct(state, job, count, struct):
    if state not in struct:
        struct[state] = {job: count}
    elif job not in struct[state]:
        struct[state][job] = count
    else:
        struct[state][job] += count