Pregunta

The app I am using doesn't allow new lines, so for this example I need my code to be written as a single line.

I tried the semicolon character but I don't know how to handle the for loops. Do I have to put a space after semicolon, or can I just piece statements without space? For the for loop, should I use a single space, 4 space, or just a tab? I am not sure if the app preserves tabs but the new lines become a single space.

So I need to do this:

statement1;statement2;forloop:;for_loop_statement1;statement3

EDIT: Actual code:

nums = [substring.split(',') for substring in s.split()]
for row in nums:
    row[1] = '0'
return ' '.join(','.join(row) for row in nums)
¿Fue útil?

Solución

This may be a better way of solving the problem (using or and __setitem__ to change the content of row). To change which item to substitute, just change the 1 in row.__setitem__(1, '0') to whatever index you'd like.

def foo(s):
    nums = [substring.split(',') for substring in s.split()];return ' '.join(','.join(row.__setitem__(1, '0') or row) for row in nums)

print foo('a,b,c d,e,f, g,h,i')

Output:

a,0,c d,0,f, g,0,i

This works since __setitem__ works in-place, and hence always returns None. Since the first statement in row.__setitem__(1, '0') or row is None, the result of the expression after or is yielded (i.e. row, which is what we want).

Otros consejos

from my terminal commandline:

>>> x=1;y=2
>>> x
1
>>> y
2
>>> for i in range(1,5):print(i);pass
... 
1
2
3
4
>>> 
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top