简体   繁体   中英

Efficient way to store arrays and check if they exists in Python

I'm trying to make a vision based reinforcement learning bot for tic-tac-toe and stuck with how could I save the data and retrieve it fast.

So saving data as lists for example start point is [0, 0, 0, 0, 0, 0, 0, 0, 0] , and starting policy for that [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] .

QUESTION IS: In what format should I save the current state and policy so I can access it fast for checking if the state already exists and for updating the policies? I was thinking of pandas and pickle , but found it a bit hard to implement with lists.

What you need seems like hashing, which standard Python classes set and dictionary use. However, the object list you need to store, is not hashable, therefore not directly available for using in these containers.

Hash tables gives O(1) complexity for accessing, which is what you need for checking regularly. But there are some difficulties presented by the data type you need to hash, here how can you overcome this.

1-) You can convert the list to a tuple, then store in a set or dictionary if you also want to assign some values to the key. This requires the O(n) conversion to tuple. Please note that you can't make changes to a tuple, you should completely replace it (which is why it is hashable, and list is not).

all_states = set()
state = tuple([0, 0, 0, 0, 0, 0, 0, 0, 0])
all_states.add(state)

2-) If you want to stick to lists, I guess making a wrapper class, then hashing should work, cannot vouch for this method to be a suggested Pythonic way of doing this.

class State:
    def __init__(self, contents):
        self.contents = contents

one_state = State([1, 2, 3])
hash(one_state) # Now it is hashable, just for checking.
                # If you can add it to a set, it is hashable.
all_states.add(state)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM