G
G
GreenX52020-08-02 14:45:37
Python
GreenX5, 2020-08-02 14:45:37

What is the fastest object to brute force search in Python?

I create a database for example fruits - 'Apple', 'Orange', 'Mandarin' so that later I can check if there is a new fruit in this list. Fruit many thousands and many thousands of times I will check by enumeration.
If I correctly understood the naguglenoe - the fastest way is to check the dictionary, because the hash table.
Can I create a dictionary of keys without values ​​so that I can then check 'Watermelon' in the dict?
If not, I can set the keys to zero. And then output the keys to check in
listkeys = dict.keys()
and check Watermelon' in listkeys
Wouldn't that lose the performance advantage of hash table lookups?
In short - you need an object with fruit for the fastest enumeration. )

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Kitaev, 2020-08-02
@GreenX5

fruits = set()
fruits.add("Apple")
fruits.add("Apple")  # Ничего не произойдёт
fruits.add("Pineapple")

assert "Apple" in fruits
assert "Watermelon" not in fruits

print(fruits)

fruits.remove("Apple")
assert "Apple" not in fruits

D
Dr. Bacon, 2020-08-02
@bacon

Can I create a dictionary of keys without values ​​so that I can then check 'Watermelon' in the dict?

Yes, it's called set.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question