user6024347
user6024347

Reputation:

How to delete an dictionay after processing it in python

I am merging 4 files which has billions of records this script is just an example of what i want

In this script im trying to adding records into four dictionaries and atlast im merging those 4 dictionaries to one dictionar

I am trying to delete 2nd dictionary after it is getting processed(after merging is done into one final dictionary) but it is throwing some error

class project(object):

    def one(self):
        self.hash_1 = {}
        self.hash_1["ramu"] = ["10","20"]
        self.hash_1["se"] = ["1","2"]

    def two(self):
        self.hash_2 = {}
        self.hash_2["ramu"] = ["0","2"]
        self.hash_2["se"] = ["111","2w"]

    def three(self):
        self.hash_3 = {}
        self.hash_3["ramu"] = ["44","22"]
        self.hash_3["se"] = ["111121","25"]

    def four(self):
        self.hash_4 = {}
        self.hash_4["ramu"] = ["4433","222"]
        self.hash_4["se"] = ["16621","2532"]

    def process(self):

        self.final_hash = {}

        for k in self.hash_1:
            self.final_hash[k] = self.hash_1[k]
            print k

            if k in self.hash_2:
                print self.hash_2[k]

            else:
                print "no"

            del self.hash_2

            if k in self.hash_3:
                print self.hash_3[k]

            else:
                print "no"

            del self.hash_3

            if k in self.hash_4:
                print self.hash_4[k]

            else:
                print "no"

            del self.hash_4

        print self.final_hash



e_obj = project()
e_obj.one()
e_obj.two()
e_obj.three()
e_obj.four()
e_obj.process()

Error:

e_obj.process()
  File "hash_remove_test.py", line 31, in process
    if k in self.hash_2:
AttributeError: 'project' object has no attribute 'hash_2'

I want to delete every dictionary after processing it ot else it is throwing memoryError (since data is big)

How to solve this problem?

Note: The whole idea is to delete every dictionary after merging it

Upvotes: 0

Views: 62

Answers (2)

Raji
Raji

Reputation: 113

If I can rephrase your question, what you want is a dictionary final_hash with "ramu" and "se" as keys and two corresponding value arrays that have all the values of ramu and se from hash_1, hash_2, hash_3 and hash_4, right? Here's how I'd do it:

def process(self):
    final_hash = dict()
    for key in self.hash_1:
            if key not in final_hash:
                    final_hash[key]= []
                    final_hash[key].append(self.hash_1[key])
    for key in self.hash_2:
            final_hash[key].append(self.hash_2[key])
    for key in self.hash_3:
            final_hash[key].append(self.hash_3[key])
    for key in self.hash_4:
            final_hash[key].append(self.hash_4[key])

    del(self.hash_1)
    del(self.hash_2)
    del(self.hash_3)
    del(self.hash_4)
    print final_hash["ramu"], final_hash["se"]

[['10', '20'], ['0', '2'], ['44', '22'], ['4433', '222']]

[['1', '2'], ['111', '2w'], ['111121', '25'], ['16621', '2532']]

I have just hard coded the process() function. If you have a lot other dictionaries that have to be merged together, you might want to consider automating that part.

Upvotes: 0

tripleee
tripleee

Reputation: 189937

Your for loop runs del self.hash_2 during its first iteration (after examining self.hash_2), so it will be gone when the second iteration starts.

Upvotes: 2

Related Questions