File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 53,
6+
"metadata": {},
7+
"outputs": [],
8+
"source": [
9+
"# How to find a list of strings in another list of strings without using for-loops (vectorized)\n",
10+
"# Via NumPy vectorization"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 54,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"import numpy as np"
20+
]
21+
},
22+
{
23+
"cell_type": "code",
24+
"execution_count": 55,
25+
"metadata": {},
26+
"outputs": [],
27+
"source": [
28+
"keys = np.array([\"the\", \"hi\", \"a\"])\n",
29+
"data = np.array([\"hello\", \"a\", \"world\", \"the\", \"python\", \"hi\", \"a\", \"sea\", \"the\"])"
30+
]
31+
},
32+
{
33+
"cell_type": "code",
34+
"execution_count": 56,
35+
"metadata": {},
36+
"outputs": [],
37+
"source": [
38+
"indices_matrix = np.core.defchararray.find(data, keys.reshape(-1,1) )\n",
39+
"indices = np.where(indices_matrix==0)[1]"
40+
]
41+
},
42+
{
43+
"cell_type": "code",
44+
"execution_count": 57,
45+
"metadata": {},
46+
"outputs": [
47+
{
48+
"name": "stdout",
49+
"output_type": "stream",
50+
"text": [
51+
"[3 8 5 1 6]\n"
52+
]
53+
}
54+
],
55+
"source": [
56+
"print(indices)"
57+
]
58+
}
59+
],
60+
"metadata": {
61+
"kernelspec": {
62+
"display_name": "Python 3",
63+
"language": "python",
64+
"name": "python3"
65+
},
66+
"language_info": {
67+
"codemirror_mode": {
68+
"name": "ipython",
69+
"version": 3
70+
},
71+
"file_extension": ".py",
72+
"mimetype": "text/x-python",
73+
"name": "python",
74+
"nbconvert_exporter": "python",
75+
"pygments_lexer": "ipython3",
76+
"version": "3.7.2"
77+
}
78+
},
79+
"nbformat": 4,
80+
"nbformat_minor": 2
81+
}

0 commit comments

Comments
 (0)