How To Get The Characters In Row And Column?
Solution 1:
This is a way to achieve what you want to do without numpy:
defprint_non_0_len_ge_1(li):
"""removes 0 from front/back of line, prints rest of line if > 1 consecutive letter
splitting at 0 between words."""for line in li:
no_zero = ''.join(line).strip("0").split("0")
for p in no_zero:
iflen(p)>1:
print(*p,sep="\n")
print("")
data = [['0', '0', '0', '0', '0', '0', '0', '0', '0', '0'],
['0', '0', '0', 'E', 'A', 'G', 'L', 'E', '0', '0'],
['0', '0', '0', '0', 'P', '0', '0', '0', '0', '0'],
['0', '0', '0', '0', 'P', '0', 'P', '0', '0', '0'],
['0', '0', '0', '0', 'L', '0', 'I', '0', '0', '0'],
['0', '0', '0', 'C', 'E', 'R', 'E', 'A', 'L', '0'],
['0', '0', '0', '0', '0', '0', '0', '0', '0', '0']]
# apply to lines
print_non_0_len_ge_1(data)
# apply to transposed data to get the columns
print_non_0_len_ge_1(zip(*data))
Output:
E
A
G
L
E
C
E
R
E
A
L
APP
L
E
PI
E
You can solve it similarily if using numpy - just remove the starting / ending 0, split at 0 and apply to normal and transposed data.
The method has a drawback - you need 0
between any non-word-forming characters in both directions to allow it to work (you can not use "EGG" starting ad "(E)agle" because you get GP
twice from it.
Solution 2:
In this case you might iterate through rows and columns (using indexing) and use list comprehension to remove 0
s. Consider following example
a = np.array([['T','E','S','T'],
['0','0','0','E'],
['0','0','0','S'],
['0','0','0','T']])
height,width = a.shape
for i inrange(height):
word = ''.join([j for j in a[i] if j!='0'])
iflen(word)>=2: print(word)
for i inrange(width):
word = ''.join([j for j in a[:,i] if j!='0'])
iflen(word)>=2: print(word)
output:
TEST
TEST
a
is hardcoded value for example clarity, note usage of a.shape
which is more elegant than magic numbers.
As noted in comment it had one flaw in original form, to avoid that problem, for
s should look following way:
for i in range(height)
words = ''.join(a[i]).split('0')
words = [i for i in words if len(i)>=2]
if words: print(words)
for i in range(width):
words = ''.join(a[:,i]).split('0')
words = [i for i in words if len(i)>=2]
if words: print(words)
Note that now words
is list, this method can also detected two or more words in one row or column.
Post a Comment for "How To Get The Characters In Row And Column?"