ETA: solved!
Very basic python question for folks what know python.
So, I'm messing about with python a bit (rev 2.7.3), and I'm doing some very basic just-above-hello-world stuff. I want to take two lines of input and merge them, but if the second line starts with a + (like some text clients do when inserting a line break), I want to delete the +. But this:
fails with this error message:
I tried using " quotes and with and without the \ escape character. What's the right syntax, here? (google was unhelpful, or at least I had too much trouble distilling the results down to things that were relevent.)
also, man, python is weird!
Very basic python question for folks what know python.
So, I'm messing about with python a bit (rev 2.7.3), and I'm doing some very basic just-above-hello-world stuff. I want to take two lines of input and merge them, but if the second line starts with a + (like some text clients do when inserting a line break), I want to delete the +. But this:
import sys
line1 = sys.stdin.readline()[:-1]
line2 = sys.stdin.readline()[:-1]
if line2[0] == '\+'
line1 = line1 + line2[1:]
print line1
fails with this error message:
File "foo.py", line 4
if line2[0] == '\+'
^
SyntaxError: invalid syntax
I tried using " quotes and with and without the \ escape character. What's the right syntax, here? (google was unhelpful, or at least I had too much trouble distilling the results down to things that were relevent.)
also, man, python is weird!
no subject
Date: 2013-07-15 07:39 pm (UTC)no subject
Date: 2013-07-15 07:45 pm (UTC)it was a stupid error but not the one I was expecting! :)
no subject
Date: 2013-07-16 01:57 pm (UTC)no subject
Date: 2013-07-17 02:35 pm (UTC)no subject
Date: 2013-07-17 02:41 pm (UTC)no subject
Date: 2013-07-17 03:09 pm (UTC)no subject
Date: 2013-07-15 08:13 pm (UTC)(the reptile, not the language)
no subject
Date: 2013-07-15 08:44 pm (UTC)no subject
Date: 2013-07-16 03:50 am (UTC)no subject
Date: 2013-07-15 09:04 pm (UTC)I've been learning it recently, and I like it a lot.
no subject
Date: 2013-07-16 02:17 am (UTC)no subject
Date: 2013-07-16 02:25 am (UTC)I STILL haven't really managed to bend my brain around lambda statements. :D
no subject
Date: 2013-07-16 02:45 pm (UTC)no subject
Date: 2013-07-17 03:18 am (UTC)no subject
Date: 2013-07-17 02:31 pm (UTC)def fn(x,y): return x + y.lstrip('+')
no subject
Date: 2013-07-18 03:25 am (UTC)no subject
Date: 2013-08-04 10:21 am (UTC)__author__ = 'chaynes' from itertools import chain, count, cycle, ifilter, repeat def primes(): sieve = [] for p in ifilter(lambda _: reduce(lambda x, isntMultipleOf: isntMultipleOf.next() and x, sieve, True), count(2)): sieve.append(cycle(chain(repeat(True, p-1), (False,)))) yield p def main(): for p in primes(): print p if __name__ == "__main__": main()the sieve list holds a list of generators, one for each prime, that generates "False" if a number is not a multiple of that prime, and "True" if it is. It really just generates the sequence (True, True, ... True, False) with a period the length of the prime, over and over.
It has to use reduce(lambda x, y: f(x) and y) instead of "all()" because any/all do lazy/short-circuit evaluation and the generators depend on being called for each integer. That's also why the "isMultipleOf" comes before the "and" and/or also short circuit.
Happy to explain further if anyone is interested.
no subject
Date: 2013-08-05 11:16 pm (UTC)Thanks!
no subject
Date: 2013-08-13 01:14 am (UTC)__author__ = 'chaynes' from heapq import heappushpop, heappush from itertools import count def primes(): """ generate a list of primes sieve contains a heap of tuples, (nextLargestMultipleOfPrime, prime) top of the heap is the smallest composite bigger or equal to our last prime we repeatedly update top of heap until either: we find our candidate, in which case it isn't a prime so we increment the candidate or the smallest composite is bigger than our candidate in which case we push the new prime and its next multiple """ sieve = [] yield 2 p = 3 while True: heappush(sieve, (p * p, p)) yield p p += 2 while sieve[0][0] <= p: if (sieve[0][0] == p): p += 2 heappushpop(sieve, (sieve[0][0] + sieve[0][1], sieve[0][1])) def main(): """ print a generated list of primes """ for (p, _) in zip(primes(), xrange(10000)): print p if __name__ == "__main__": main()Enjoy! and as always happy to answer questions. BTW I just installed Python and Emacs on my phone! I love living in the future.no subject
Date: 2013-07-16 02:37 pm (UTC)print sys.stdin.readline()[:-1] + sys.stdin.readline()[:-1].lstrip('+')