0

I have a dictionary with data similar to what is below

{'Bldg1':[('0', 'Sam'), ('0', 'John'), ('1', 'Tom'), ('2', 'Jane')], 'Bldg2' :[('0', 'Peter'), ('0', 'Dan'), ('1', 'Tom'), ('1', 'Jack'), ('3', 'Frank')]}

I am trying to combine tenants on the same floor of a building. Except for the Tenant, there is duplicate data like floor '0' in 'Blgd1' . I am trying take the duplicate data and combine the second value to look similar to the output below:

{'Bldg1':[('0', 'Sam - John'), ('1', 'Tom'), ('2', 'Jane')], 'Bldg2' :[('0', 'Peter - Dan'), ('1', 'Tom - Jack'), ('3', 'Frank')]}

I have been working with the function below and it will remove duplicate entries, but I am stuck moving forward. Is this even possible?

 def get_tenants_bldg_floor(dct, building, floor):
    unique_tenant_list = []
    for k,v in dct.iteritems():
        building = k
        for item in v:
            floor = item[0]
            tenant = item[1]
            ten_bldg_floor = tenant+', '+bldg_in_func+', '+floor_in_func
            for v in k:
                if ten_bldg_floor in unique_tenant_list:
                    print 1
                else:
                    unique_tenant_list.append(ten_bldg_floor)
                    print 0
    return unique_tenant_list
4
  • what are the purpose of the function inputs "building" and "floor"? I also don't understand what you mean by duplicate data. Could you add more details? Commented Jul 17, 2014 at 17:16
  • I originally pasted wrong function. Building and floor are variables from inside the function - I was told I could do this by a mentor. The duplicate data is building and floor. I am trying to combine tenants on the same floor of a building. Commented Jul 17, 2014 at 17:21
  • 3
    @Waitkus what do you mean "stuck"? What does your code do, and what does it not do? Of course it's possible, but other data structures may make it easier - how about {building: {floor: [tenant, list]}} Commented Jul 17, 2014 at 17:23
  • @jonrsharpe I mean stuck as I dont know where to go from here. What my code does is it imports data from several CSV files to get me to data that will be used to correlate tenants to building and floor. With the current structure, it will make the list 3-4 times as big as it needs to be which is why I am trying to reduce. The dictionary you propose is perfect but I am not sure how to get there. Commented Jul 17, 2014 at 17:29

1 Answer 1

1

I would do as @jonrsharpe suggests and combine the floors into a dictionary. That makes it MUCH easier to deal with, too:

original_data = {'Bldg1':[('0', 'Sam'), ('0', 'John'), ('1', 'Tom'), ('2', 'Jane')], 'Bldg2' :[('0', 'Peter'), ('0', 'Dan'), ('1', 'Tom'), ('1', 'Jack'), ('3', 'Frank')]}

def combine_by_floor(data):
    new_data = dict()
    for building, floortenants in data.items():
        for floor,tenant in floortenants:
            new_data.setdefault(building, {}).setdefault(floor, []).append(tenant)
    return new_data

combined = combine_by_floor(original_data)
### EXAMPLE ###
>>> print(combined)
{'Bldg1': {'2': ['Jane'], '1': ['Tom'], '0': ['Sam', 'John']}, 'Bldg2': {'3': ['Frank'], '1': ['Tom', 'Jack'], '0': ['Peter', 'Dan']}}

Basically, run through your dictionary, and make a new one. For each tuple, append tenant to new_data[building][floor]. If new_data[building] does not exist, create it as an empty dict. If new_data[building][floor] does not exist, create it as an empty list.

Sign up to request clarification or add additional context in comments.

2 Comments

In the new dictionary, the building is the key, the floor is the value and what does the tenant become if i wanted to reference them? for k,v in dct.iteritems(): bldg_name = k for item in v: floor = item
@Waitkus: in the new dictionary, the key is the building, and the value is ANOTHER DICTIONARY, whose keys are floors and whose values are tenants. In other words: for bldg, floortenants in dct.items(): for floor, tenants in floortenants.items():

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.