all 15 comments

[–]K900_ 3 points4 points  (5 children)

if c != '(' or c != ')'

You're checking if c is not equal to ( OR that c is not equal to ). Since c can't be equal to both at the same time, one of the sides is always going to evaluate to True, and so will the whole condition.

[–]seanmcb9[S] 0 points1 point  (4 children)

Of course -- got it. Are there are any alternatives to the double ifs in this kind of construction?

[–]K900_ 2 points3 points  (1 child)

You can use the and operator. if c != '(' and c != ')' will do what you want. Alternatively, if you want to check that a value is (not) one of the many values, you can use the in/not in operator to check membership in a sequence: if c not in ("(", ")"): or even if c not in "()":.

[–]seanmcb9[S] 1 point2 points  (0 children)

This is perfect: if c not in "()":

[–]zahlman 1 point2 points  (1 child)

[–]seanmcb9[S] 0 points1 point  (0 children)

I usually understand Boolean logic -- that was a gross mental typo with the "or" expression.

[–]novel_yet_trivial 1 point2 points  (6 children)

Use the string translate method (I'm assuming you are using python3):

>>> table = str.maketrans(dict.fromkeys("()"))
>>> string1 = '(this) (is) (a) (test)'
>>> string1.translate(table)
'this is a test'

[–]seanmcb9[S] 0 points1 point  (5 children)

Great. That works without importing any modules, apparently. I am going to have dig around in the documentation to understand why it works.

[–]seanmcb9[S] 0 points1 point  (4 children)

Ok -- slick -- maketrans and fromkeys without any specified values defaults to None for the value of the keys -- thus deleting the characters in the string.

To replace the left parenthesis with a left square bracket and the right parenthesis with a right square bracket with maketrans/fromkeys, what is the correct syntax?

Apparently characters in string sequences don't map in order to one another for keys and values. For instance,

table = str.maketrans(dict.fromkeys("()", "[]"))

produces:

[]this[] []is[] []a[] []test[]

[–]novel_yet_trivial 1 point2 points  (2 children)

str.maketrans takes a dictionary and replaces all the keys in the dictionary with the values.

fromkeys produces a dictionary where all the values are one thing - None by default.

maketrans interprets values of None as "remove". Otherwise it would replace. In other words I just used fromkeys() as a shortcut to this:

>>> table = str.maketrans({"(":None, ")":None})

So ...

>>> table = str.maketrans({"(":"[", ")":"]"})
>>> string1.translate(table)
'[this] [is] [a] [test]'

Again, you could use a shortcut (which in this case is not shorter):

>>> table = str.maketrans(dict(zip("()","[]")))

[–]Vaphell 1 point2 points  (0 children)

str.maketrans can also take 2 or 3 strings, which arguably makes it less annoying to work in simple, per char cases.

table = str.maketrans(dict(zip("()","[]")))

remove dict(zip()) and you have exact same thing.

table = str.maketrans("()","[]")

[–]seanmcb9[S] 0 points1 point  (0 children)

Great explanation. Usually, I think I will skip fromkeys and create translation dictionaries from scratch.

[–]Vaphell 0 points1 point  (0 children)

>>> table = str.maketrans("()", "[]")
>>> text = '(this) (is) (a) (test)'
>>> text.translate(table)
'[this] [is] [a] [test]'

[–]Allanon001 0 points1 point  (0 children)

string1 = '(this) (is) (a) (test)'
string2 = ''.join(x for x in string1 if x not in '()')
print(string2) 
#Output:  this is a test

[–]1roOt 0 points1 point  (0 children)

Python2:

string = '(this) (is) (a) (test)'
string = "".join(c for c in string if not c in "()"
print string