-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstrcmp_matlab.py
96 lines (78 loc) · 2.07 KB
/
strcmp_matlab.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Apr 11 11:11:15 2020
python version of Matlab strcmp()
@author: dpg
"""
def strcmp(stringarray, teststr):
zz = []
if len(stringarray) > 1:
for nn in range(len(stringarray)):
if stringarray[nn] is teststr:
zz.append(True)
else:
zz.append(False)
elif len(stringarray) == 1 :
if stringarray is teststr:
zz.append(True)
else:
zz.append(False)
else:
zz = []
return zz
def strcmp_int(stringarray, teststr):
"""
integer version of Matlab strcmp, returns integer indices where teststr found in stringarray
"""
zz = []
if len(stringarray) > 1:
for nn in range(len(stringarray)):
if stringarray[nn] is teststr:
zz.append(nn)
elif len(stringarray) == 1 :
if stringarray is teststr:
zz.append(0)
else:
zz=[]
else:
zz = []
return zz
def strfilter_logical(stringarray, logicalindex):
"""
Parameters
----------
stringarray : string
DESCRIPTION.
logicalindex : logical
DESCRIPTION.
Returns stringarray filtered by logicalindex
-------
TYPE
stromg.
"""
resultstr = []
if len(stringarray) != len(logicalindex):
print("error: logical index not same length as string\n")
return []
else:
for n in range(len(stringarray)):
if logicalindex[n]:
resultstr.append(stringarray[n])
return resultstr
def strfilter(stringarray, index):
"""
Parameters
----------
sringarray : string
DESCRIPTION.
index : list of integers or tuple
DESCRIPTION.
Returns
-------
stringarray filtered by integer (locational) index
"""
resultstr = [stringarray[nn] for nn in index]
# for nn in index: #this code does the same thing...
# resultstr.append(stringarray[nn])
return resultstr