opinion for newbie code

R

robsom

I have strings of like this:
"one two : three four ;\n"
and I want to put them into lists using ":" as delimeter but I have to
get rid of all the spaces and other not necessary characters, like this:
list = ["onetwo", "threefour"]

so I wrote the following code, which works, but I have a couple of
questions:

1. for line in fin.readlines():
2. eq = []
3. row = line.replace(" ","").replace(";","").replace("\n","").split(":")
4. # ...
5. # other code
6. # result of the code are three lists A, B, C
7. # which I want to put into a single list L = ['A','B','C']
8. # ...
9. L.append(A)
10. L.append(B)
11. L.append(C)

Question n.1: what do you think of statement number 3? it works but I
kind of suspect it is not the best way to do it.

Question n.2: on line 9-11 I add my three lists (A, B, C) to the L list
using three append instructions. Is there another way to do the same
thing, i.e. to add more than one element to a list with a single
instruction?
thanks

R
 
E

Erik Max Francis

robsom said:
Question n.2: on line 9-11 I add my three lists (A, B, C) to the L
list
using three append instructions. Is there another way to do the same
thing, i.e. to add more than one element to a list with a single
instruction?

L = A + B + C
 
D

David M. Wilson

robsom said:
3. row = line.replace(" ","").replace(";","").replace("\n","").split(":")
Question n.1: what do you think of statement number 3? it works but I
kind of suspect it is not the best way to do it.

It is common practise, that's not to say it's good practise. An
alternative way of doing this might be:

row = re.split(r'[\s;:]+', line)

If you have never encountered regular expressions before, then lucky
you. They do have a tendancy to be efficient, and thats probably the
only time you should need them.

To save you a years' Googling, here's what the above does:

[\s;:]
Match any character that is whitespace (\s), a semicolon, or
a colon.

+
One or more times.

re.split(pattern, data)
Split <data> at every occurrence of <pattern>, returning the
result as a list.


For added performance, using "compiled = re.compile(pattern)", then
passing <compiled> instead of <pattern> will increase speed.


David.
 
P

Peter Otten

robsom said:
I have strings of like this:
"one two : three four ;\n"
and I want to put them into lists using ":" as delimeter but I have to
get rid of all the spaces and other not necessary characters, like this:
list = ["onetwo", "threefour"]

so I wrote the following code, which works, but I have a couple of
questions:

1. for line in fin.readlines():
2. eq = []
3. row = line.replace(" ","").replace(";","").replace("\n","").split(":")
4. # ...
5. # other code
6. # result of the code are three lists A, B, C
7. # which I want to put into a single list L = ['A','B','C']
8. # ...
9. L.append(A)
10. L.append(B)
11. L.append(C)

Question n.1: what do you think of statement number 3? it works but I
kind of suspect it is not the best way to do it.

Outside the for loop:

In the for loop:
['onetwo', 'threefour']
Question n.2: on line 9-11 I add my three lists (A, B, C) to the L list
using three append instructions. Is there another way to do the same
thing, i.e. to add more than one element to a list with a single
instruction?

L.extend([A, B, C])

However, I suppose this is slightly less efficient, as you have to build the
intermediate list [A, B, C].

Peter
 

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

Forum statistics

Threads
474,183
Messages
2,570,965
Members
47,513
Latest member
JeremyLabo

Latest Threads

Top