Back

Explore Courses Blog Tutorials Interview Questions

Explore Tech Questions and Answers

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

0 votes
2 views
by (19.9k points)

I've got a numpy array of strings (str8192) where the second column is the names of things. For the sake of this lets say this array is called thingList. I have two strings, string1 and string2. I'm trying to get a list of every item in the second column of thingList that is in string1 or in string 2. Currently I have this running with a for loop, but I was hoping there was a faster way I don't know about, I'm pretty new to programming.

Once I find a match, I also want to record what is in the first column but the same row as the match.

Any help to speed this is greatly appreciated, as thingList is pretty large and this functions is run quite a lot with various arrays.

tempThing = []

tempCode = []

for i in range(thingList.shape[0]):

        if thingList[i][1].lower() in string1.lower() or thingList[i] [1].lower() in string2.lower():

            tempThing.append(thingList[i][1])

            tempCode.append(thingList[i][0])

This code works fine, but it definitely is the bottleneck in my program and is slowing it down a lot.

1 Answer

0 votes
by (25.1k points)

You should use list comprehensions as they are faster than traditional loops:

result = [[x[0], x[1]] for x in thing_list if (x[1].lower() in st1_lower) or (x[1].lower() in st2_lower) ]

Related questions

0 votes
1 answer
asked May 9, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer
0 votes
1 answer

Browse Categories

...