Removing symbols and spaces in strings without using if statements?

Question:

So I need to go through a loop and apply conditional statement in every element inside the matrices’ rows.
conditions need to be checked:

  1. whether the elements is alphabet or not, if yes append it if not go for next conditional checking
  2. next checking should be checking if its symbol/non-alphabet AND its next element(i used j+1 for indexing) is also symbol/non-alphabet, if this is true then append a ‘ ‘(one blank space). but when I run this, it will stumbles on ‘list index out of range’ error due to last elements of every row cant access j+1 index. then, i created another pre-loop to check if the elements is last element or not. if its the last element go for next loop for simple checkup.
 clinput2=[['T', 'h', 'i', 's', '$', '#', 'i'], 
           ['s', '%', ' ', 'S', 'p', 'a', 'r'], 
           ['t', 'a', '#', ' ', ' ', '%', '!']]

for j in range(len(clinput2[0])):
    # print (clinput2[i][j])
    
    if (clinput2[i][j].isalpha()):
        cara.append(clinput2[i][j])

    elif (not clinput2[i][j].isalpha() ):
       if j+1<len(clinput2[i]):

        if (not clinput2[i][j+1].isalpha() or clinput2[i][j+1]==' '):
            cara.append(' ')
            j+=1
            
        
    elif (not clinput2[i][j].isalpha() or clinput2[i][j]==' '):
        pass

desired output: This is Sparta

so how do i do this without if statement?, for now I’m able to do it with if statement, but the requirement is not to use if statement.

i tried this way of conditional statement but cant find a way to nest the condition, passing a function in the paremeter would be too dirty and too long:

['pass', cara.append(clinput2[i][j])][clinput2[i][j].isalpha()]

the original method:

print ["no", "yes"][x > y]

How do I do this without if statement?

Asked By: shanksfk

||

Answers:

We can easily avoid conditional statements with the help of regular expressions. In particular,

import re
clinput2=[['T', 'h', 'i', 's', '$', '#', 'i'], 
           ['s', '%', ' ', 'S', 'p', 'a', 'r'], 
           ['t', 'a', '#', ' ', ' ', '%', '!']]

mat_str = ''.join(c for row in clinput2 for c in row)
print(re.sub(r'[^a-zA-Z]+',' ',mat_str))

mat_str is the string formed by concatenating all characters of all the rows of the matrix, in order. The expression r'[^a-zA-Z]+' matches any contiguous string of non-alphabetic characters, and the sub function replaces each such string with a blank space, ' '.

Answered By: Ben Grossmann

Just use a terrible conditional.

import itertools

clinput2 = [['T', 'h', 'i', 's', '$', '#', 'i'], 
            ['s', '%', ' ', 'S', 'p', 'a', 'r'], 
            ['t', 'a', '#', ' ', ' ', '%', '!']]
clinput2 = list(itertools.chain(*clinput2))

output = ''
for char, next_char in itertools.zip_longest(clinput2, clinput2[1:]):
    output += (
        (char.isalpha() and char) or
        (not char.isalpha() and next_char is not None and not next_char.isalpha() and ' ') or
        (''))
    
print(output)
Answered By: Bill Lynch
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.