.split() Qeustion

Alister alister.ware at ntlworld.com
Wed Aug 14 09:29:52 EDT 2013


On Wed, 14 Aug 2013 11:31:01 +0100, Joshua Landau wrote:

> On 14 August 2013 09:30, Alister <alister.ware at ntlworld.com> wrote:
>> On Tue, 13 Aug 2013 22:12:56 -0700, Gary Herron wrote:
>>
>>> On 08/13/2013 09:51 PM, eschneider92 at comcast.net wrote:
>>>> How can I use the '.split()' method (am I right in calling it a
>>>> method?) without instead of writing each comma between words in the
>>>> pie list in the following code? Also, is there a way to use .split
>>>> instead of typing the apostrophes? Thank you.
>>>>
>>>> import random pie=['keylime', 'peach', 'apple', 'cherry', 'pecan']
>>>> print(random.choice(pie))
>>>>
>>>> Eric
>>>
>>> I think you are referring to this:
>>>      pie = 'keylime peach apple cherry pecan'.split()
>>>
>>> While it's easier to type, and does save a few characters, I think the
>>> original list is clearer to a reader of your program.
>>>
>>> Gary Herron
>>
>> I would agree with the last statement.
>> Please write list definitions as lists rather than taking a short-cut
>> to save a few key presses
> 
> That's true with this example, but is:
> 
> lines = [
>     "Developments in high-speed rail, and high-speed", "transport more
>     generally, have historically been", "impeded by the difficulties in
>     managing friction", "and air resistance, both of which become",
>     "substantial when vehicles approach high speeds.", "The vactrain
>     concept eliminates these obstacles", "by employing magnetically
>     levitating trains in", "tubes kept at a complete vacuum, allowing
>     for", "heoretical speeds of thousands of miles per", "hour. The high
>     cost of constructing such a system,",
>     "however, and the difficulty of maintaining a", "vacuum over large
>     distances, has prevented this", "type of system from ever being
>     built. The", "Hyperloop can be viewed as a modified vactrain,",
>     "employing more cost-effective solutions to the", "same problems the
>     latter was designed to solve."
> ]
> 
> really more readable than:
> 
> lines = """\
> Developments in high-speed rail, and high-speed transport more
> generally, have historically been impeded by the difficulties in
> managing friction and air resistance, both of which become substantial
> when vehicles approach high speeds.
> The vactrain concept eliminates these obstacles by employing
> magnetically levitating trains in tubes kept at a complete vacuum,
> allowing for heoretical speeds of thousands of miles per hour. The high
> cost of constructing such a system,
> however, and the difficulty of maintaining a vacuum over large
> distances, has prevented this type of system from ever being built. The
> Hyperloop can be viewed as a modified vactrain,
> employing more cost-effective solutions to the same problems the latter
> was designed to solve.
> """[1:-1].split("\n")
> 
> ?
Yes, because I can see at the start that a list is being created & can 
skip over the data top the next line of code. I could easily miss 
the .split() at the end of the string deffinition.




-- 
"It ain't over until it's over."
-- Casey Stengel



More information about the Python-list mailing list