Jump to content

Python 3 search in multiple array style things to find item

Hi there,

I am building a system for storage which will most likely have a raspberry pi as the host. How would I make it so I could easily search in all “boxes” and then see if they have the item that I am looking for. I would be using Python 3

#CODE EXAMPLE THINGY#

Box1 [car,dog,sheep]

 

Box2 [rock,cat,sheep]

Input for search “sheep”

Then searches all boxes I have made to see if it that has sheep in them

If it has them then it goes:

OUTPUT: Sheep in box 1 and 2

Else goes item unavailable.

Input for search “car”

Searches all boxes I have made to see if it has car in them*

If it has them then it goes:

OUTPUT: Car in box 1

 

This is an example and colours, led and items in box would be different.

I don’t mind just needing to use the terminal in order to find an item that is required.

If you have any questions feel free to ask and I will try and get back to you ASAP.

I am not asking for someone to code the entire thing I am just asking if anyone knows any systems that have been made like this and if there is any ideas on what systems I should use.

If you want to reach me privately my discord is Spoiled_Kitten#4911

Thanks,

Blake McCullough

 

 

Blake has arrived!!

Just your local tech geek!

Love to help!

Link to comment
Share on other sites

Link to post
Share on other sites

Can we get more context? Python can do this, you just have to point it to where to look

Community Standards || Tech News Posting Guidelines

---======================================================================---

CPU: R5 3600 || GPU: RTX 3070|| Memory: 32GB @ 3200 || Cooler: Scythe Big Shuriken || PSU: 650W EVGA GM || Case: NR200P

Link to comment
Share on other sites

Link to post
Share on other sites

I imagine something like this

 

item = input()
output = []
for i in boxes:
     if item in i:
          output.append(i)
print(output)

 

Link to comment
Share on other sites

Link to post
Share on other sites

Tbf this sounds like it would be better put in a database of some sort, it'll be far faster then looping over everything to see if an item is in an array. 

                     ¸„»°'´¸„»°'´ Vorticalbox `'°«„¸`'°«„¸
`'°«„¸¸„»°'´¸„»°'´`'°«„¸Scientia Potentia est  ¸„»°'´`'°«„¸`'°«„¸¸„»°'´

Link to comment
Share on other sites

Link to post
Share on other sites

The simplest solution would be just looping over your lists:

 

if len([item for item in box1 if item == search]) > 0:
	print(f"{search} is in box1")

Then again for the other boxes.

 

However, this will get slow real fast if you're storing a large amount of items. You can speed this up by sorting the data and using other search algorithms. Binary search is O(log(n)) as apposed to O(n) in this linear search. Another approach would which has been stated is using a DB. At least that also takes care of storing the boxes data when your program isn't running.

 

Personally (because I like them) I'd use flask or django. Serve a simple web app that can search a DB and provide other functionality with the added bonus of it being available on your local network. You can get something like this set-up in no time at all. 

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 3/17/2021 at 6:20 PM, vorticalbox said:

Tbf this sounds like it would be better put in a database of some sort, it'll be far faster then looping over everything to see if an item is in an array. 

yeah i ended up doing it i can share if anyone is interested

Blake has arrived!!

Just your local tech geek!

Love to help!

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×