turn text lines into a list

X

Xah Lee

i have a large number of lines i want to turn into a list.
In perl, i can do

@corenames=qw(
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corenames);

----------
is there some shortcut to turn lines into list in Python?

Xah
(e-mail address removed)
∑ http://xahlee.org/
 
R

Reinhold Birkenfeld

Xah said:
i have a large number of lines i want to turn into a list.
In perl, i can do

@corenames=qw(
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corenames);

str.splitlines
 
G

Gunnar Hjalmarsson

Xah said:
i have a large number of lines i want to turn into a list.
In perl, i can do

@corenames=qw(
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
);

Impractical to mix code and data, isn't it?

chomp( my @corenames = <DATA> );

__DATA__
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
 
M

Matthias Huening

Xah Lee (27.06.2005 12:33):
i have a large number of lines i want to turn into a list.
In perl, i can do

@corenames=qw(
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corenames);

txt = """rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile"""

print txt.splitlines()

Matthias
 
G

Grant Edwards

i have a large number of lines i want to turn into a list.
In perl, i can do

@corenames=qw(
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corenames);

corenames = [ "rb_basic_islamic",
"sq1_pentagonTile",
"sq_arc501Tile",
"sq_arc503Tile"]
 
F

F. Petitjean

[En-tête "Followup-To:" positionné à comp.lang.python.]
Le Mon, 27 Jun 2005 14:27:28 -0000, Grant Edwards a écrit :
i have a large number of lines i want to turn into a list.
In perl, i can do

@corenames=qw(
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corenames);

corenames = [ "rb_basic_islamic",
"sq1_pentagonTile",
"sq_arc501Tile",
"sq_arc503Tile"]
Another way : (less typing of quotes)

all_names = """
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
"""

corenames = all_names.split()

Regards.
 
B

Bengt Richter

[En-tête "Followup-To:" positionné à comp.lang.python.]
Le Mon, 27 Jun 2005 14:27:28 -0000, Grant Edwards a écrit :
i have a large number of lines i want to turn into a list.
In perl, i can do

@corenames=qw(
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
);

use Data::Dumper;
print Dumper(\@corenames);

corenames = [ "rb_basic_islamic",
"sq1_pentagonTile",
"sq_arc501Tile",
"sq_arc503Tile"]
Another way : (less typing of quotes)

all_names = """
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
"""

corenames = all_names.split()

Of course, the lines better not have embedded spaces or they'll be split
into several lines. For lines per se, probably I'd do

corenames = """\
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile
""".splitlines()

Note the \ to avoid a blank leading line.
... solid
... embedded space
... leading
... trailing
... both
... """.splitlines()
['solid', 'embedded space', ' leading', 'trailing ', ' both ']

Regards,
Bengt Richter
 
B

Big and Blue

Gunnar said:
Impractical to mix code and data, isn't it?

Obviously not impractical, given he did it quite easily and succinctly.
chomp( my @corenames = <DATA> );

__DATA__
rb_basic_islamic
sq1_pentagonTile
sq_arc501Tile
sq_arc503Tile

Not so easy when you have multiple variables to set. And the original
version was transparent in what it was doing - your version is not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top