【问题标题】:Comparing more nested dictionaries (save time)比较更多嵌套字典(节省时间)
【发布时间】:2015-08-12 13:27:44
【问题描述】:

我需要一个建议,如何比较未知数量的嵌套字典以尽可能降低时间消耗。

下面是例子:

我有来自房屋租赁公司的数据。一所房子可以在更多的这些机构中。有一些关于这些房子的信息。

1.国家

2。日期

3.其他信息,例如房间数量等。

这些数据是这样存储的:

DictionaryOnFirstLevel: key = Country, value = DictionaryOnSecondLevel
DictionaryOnSecondLevel: key = Date, value = instance of class House including price, Country, Date etc.

所以,我想要的是找到相同的房子(同样的两间房子不是同一个对象)并比较它们的价格和另一个数据。

因为我知道一个国家和日期,所以我不必将每个房子与每个房子进行比较 - 我不必比较来自爱尔兰的物品和来自土耳其的物品,同样的问题是日期 - 我不需要必须比较具有不同日期的对象。

for date in first_agency.house_dict['Ireland']:
    for h1 in first_agency.house_dict['Ireland'][date]:
        if second_agency.house_dict['Ireland'].has_key(date): # to save some time
            for h2 in second_agency.house_dict['Ireland'][date]:
                if h1.equals(h2): # method equals do some approximative comparison of names of houses and other attributes
                     #do some calculations and stuff

以上代码仅适用于 2 个机构(第 1 和第 2)和国家“爱尔兰”。我只得到那些在 'Ireland' 键中的第一个机构中的日期,所以我不必与其他日期一起工作,因为它们没有机会值得。

那么你能帮我改进我的代码,以便我可以比较所有机构吗?

【问题讨论】:

  • 你知道你的例子不是有效的python代码吗?
  • @muddyfish 你为什么这么认为?也许我写错了,但我找不到...
  • 你有一个变量1st。任何变量都不能以数字开头。
  • @muddyfish 对象比较当且仅当它们具有相同的国家和日期。
  • @muddyfish 好的,很抱歉,这只是一个示例,我现在会更正。

标签: python dictionary time-complexity nested-loops


【解决方案1】:

首先,我认为许多嵌套 if + for 循环的方法是一种不好的方法,因为您重复一堆代码并且很难测试。

你最好把它分解成几个函数

def get_all_duplicate_houses_on_date(date):
    """return a list of duplicate house lists (list within list)"""
    houses = get_all_houses_on_date(date)
    all_duplicates = []
    for house in houses:
         all_duplicate_lists.append(get_duplicates(house, houses)
    return all_duplicates

def get_duplicates(house, houselist):
    """return all duplicates of house in houselist"""
    duplicates = []
    for other_house in houselist:
        duplicates.append(other_house) if house.equal(other_house)
    return duplicates

def get_all_houses_on_date(date):
    """return all houses in every country on a given date"""
    all_houses = []
    for country in all_counties:
        all_houses.extend(get_all_houses_from_country(country, date))
    return [h for h in all_houses 

def get_all_houses_from_country_on_date(country, date):
    """returns all houses from all agencies in a given country
       and on a given date"""
    country_houses = []
    for agency in all_agencies:
        all_houses.extend(get_agency_houses_in_country(agency, country, date)
    return country_houses

def get_agency_houses_in_country_on_date(agency, country, date):
    """returns all houses from the given agency in a given country
       on a given date. Or return an empty list"""
    return agency.get(country, {}).get(date, [])

现在这完全被破解了,我还没有验证这里没有任何重大缺陷,但重点不在于代码的细节;关键是您创建了几个可测试 函数,每个函数执行一个独特的任务。一旦你有了一个重复的列表,你就可以做任何你想做的事情,但这应该完全是另一个功能。

在优化方面,单独尝试和优化上述任何一种功能可能要容易得多。看起来我上面的天真实现就像 O(n^n) 时间费用;不是很好。我最好的猜测是,您可能会将其降低到 O(n^2),但这可能需要一些创造力。

【讨论】:

  • 好的,我去试试。根据您的最后一部分 - 我认为时间复杂度不是这里的重点,因为有一些房屋具有相同的日期和国家,因此应该关注使用这些信息过滤它们。如果您知道我的意思,我认为有时 O(n^n) 和 O(n^n) 之间存在很大差异。具有相同国家和日期的房屋数量很少,实际上可以认为是一个常数。这是我的想法,可能是错误的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-22
  • 2018-07-17
  • 2018-09-19
  • 2017-02-16
相关资源
最近更新 更多