Pattern Search Regular Expression
Denis McMahon
denismfmcmahon at gmail.com
Sat Jun 15 09:41:21 EDT 2013
On Sat, 15 Jun 2013 11:55:34 +0100, Mark Lawrence wrote:
> >>> sentence = "By the new group"
> >>> words = sentence.split()
> >>> words[words[0],words[-1]]
> Traceback (most recent call last):
> File "<stdin>", line 1, in <module>
> TypeError: list indices must be integers, not tuple
>
> So why would the OP want a TypeError? Or has caffeine deprivation
> affected your typing skills? :)
Yeah - that last:
words[words[0],words[-1]]
should probably have been:
first_and_last = [words[0], words[-1]]
or even:
first_and_last = (words[0], words[-1])
Or even:
first_and_last = [sentence.split()[i] for i in (0, -1)]
middle = sentence.split()[1:-2]
--
Denis McMahon, denismfmcmahon at gmail.com
More information about the Python-list
mailing list