Find a word in a numpy array [duplicate] - python-3.x

This question already has answers here:
Numpy 'where' on string
(4 answers)
Closed 4 years ago.
word2 = "find"
np.where(dictionary == word2)[0]
The dictionary variable is a numpy array with thousands of words in the english dictionary, when looking for the index of a word, numpy also returns the index of all the strings containing word2 as a substring.
Does anyone has an idea of how to fix this? I would like to return just the perfect string match
Thanks

a = np.array(['apple', 'orange', 'apple', 'banana'])
arr_index = np.where(a == 'apple')
print arr_index
print a[arr_index]
Refer this post

Related

Interpret the meaning of one line of Python3 code [duplicate]

This question already has answers here:
is_max = s == s.max() | How should I read this?
(5 answers)
Closed last month.
I am a beginner for Python3. Here I have one question to bother you. In the function below:
def highlight_min(s):
'''
highlight the minimum in a Series red.
'''
is_max = s == s.min()
return ['background-color: red' if v else '' for v in is_max]
What does is_max = s == s.min() mean?
Thank you.
s appears to be an array of some sort. The result of is_max in this operation will be a boolean type array the same size as s. Any element in s that is the same as the minimum of the array s will have the value True, else it will have the value False .
The following line is a loop through is_max that returns a python list. if v queries if an element is True in is_max and assigns the string 'background-color: red' if it is or an empty string if not.
I suspect that the function was copied from the original probably called 'highlight_max' and is_max should really be is_min.

For-loop skipping a term when searching for list of substrings within a list of strings [duplicate]

This question already has answers here:
Python list.remove() skips next element in list
(6 answers)
Closed 1 year ago.
I need to remove from my_list anything that contains any substring from banned (both lists are very long, approximately 30k+).
Here's a mini example:
my_list = ['dnb','distinctive_group_inc','real-estate-profile','estate.com']
banned = ['distinctive','estate']
Desired outcome:
my_list = ['dnb']
I'd like to be able to do it in one go so I tried:
my_list = [i for i in banned if i not in my_list]
Since that didn't work at all, I tried it in two steps:
for i in reversed(my_list):
for j in banned:
if j in i:
my_list.remove(i)
else:
pass
How can I do it in one step?
If you like list comprehension (and I do), then you were on the right track. This takes your starting comprehension but changes the criteria slightly to to exclude words that have any partial match in the banned list.
my_list = ['dnb','distinctive_group_inc','real-estate-profile','estate.com']
banned = ['distinctive','estate']
my_new_list = [
word for word in my_list
if not any(banned_word in word for banned_word in banned)
]
print(my_new_list)
Should result in:
['dnb']

How can I create a list from a to z, and A to Z [duplicate]

This question already has answers here:
Alphabet range in Python
(8 answers)
Closed 2 years ago.
I want to create a list from a to z, and from A to Z. Plese help me!
You can use the string module, with string.ascii_uppercase and string.ascii_lowercase.
If you type:
import string
string.ascii_uppercase
You get:
'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
If you type:
string.ascii_lowercase
You'll get the same result, but in lowercase.
To solve your problem you can do:
import string
upper_case = list(string.ascii_uppercase)
lower_case = list(string.ascii_lowercase)
upper_case and lower_case will be both lists ranging from a to z.

How to convert list to strings in python? [duplicate]

This question already has answers here:
How to convert list to string [duplicate]
(3 answers)
Closed 3 years ago.
I have this list:
x = ['nm0000131', 'nm0000432', 'nm0000163']
And I would like to convert it to:
'nm0000131',
'nm0000432',
'nm0000163'
e.g: I would like convert a list of strings (x) to 3 independent strings.
If you want three separate string you can use for loop.
Try the following code:
x = ['nm0000131', 'nm0000432', 'nm0000163']
for value in x:
print(value)
Output will be like:
nm0000131
nm0000432
nm0000163
The following code will display an output like "nm0000131" ,"nm0000432" ,"nm0000163":
x = ['nm0000131', 'nm0000432', 'nm0000163']
str1 = '" ,"'.join(x)
x = '"'+str1+'"'
print(x)
As you mentioned in the comment I would like to include some more points to my answer.
If you want to get the key-value pairs then try the following code.
y = {'131': 'a', '432': 'b', '163': 'c'}
w = []
for key, value in y.items():
w.append(value)
print(w)
Output:
['c', 'a', 'b']

How can I convert a string to a list in Python using a for loop? [duplicate]

This question already has answers here:
String to list in Python
(5 answers)
Closed 6 years ago.
Here's what I have:
def reverse(text):
opposite = []
for character in text:
stuff = opposite.append(character)
return stuff
Is there something wrong with this for loop?
Couldn't you use the split method instead it will return a list
text.split()
Or even
list(text)
Which would list out each character
yourstring='welcome'
l=[]
for char in yourstring:
l.append(char)
print l

Resources