Create list from string

E

ericdaniel

Hi,

I'm new to Python and I need to do the following:

from this: s = "978654321"
to this : ["978", "654", "321"]

Any help is appreciated

Thanks,

Eric
 
B

Ben Sizer

Hi,

I'm new to Python and I need to do the following:

from this:   s = "978654321"
to this :      ["978", "654", "321"]

What are your criteria for splitting this string? Every 3 characters?
If there isn't an even multiple of 3, which group should be shorter,
the first, the last, or maybe some other?

And do you even really need this as a string at all? Perhaps you
really just wanted to format the output of an integer? (I think that
may be done via the locale, but I am not sure.)

Often it's best to specify why you want to do something, as when using
a new language there is often a better way to achieve what you want
than the first way that occurs to you.
 
P

Peter Otten

ericdaniel said:
I'm new to Python and I need to do the following:

from this: s = "978654321"
to this : ["978", "654", "321"]

Use a loop:
s = "978654321"
items = []
for start in range(0, len(s), 3):
.... items.append(s[start:start+3])
....['978', '654', '321']

or a list comprehension:
[s[start:start+3] for start in range(0, len(s), 3)]
['978', '654', '321']

Peter
 
C

Chris

Hi,

I'm new to Python and I need to do the following:

from this:   s = "978654321"
to this :      ["978", "654", "321"]

Any help is appreciated

Thanks,

Eric

What you could do is iterate over the string appending the characters
1 at a time to a new string and when you hit the point you want to
place it in your output list you can append it there and clear the
temp string eg.

length = 3
tmp_string = ''
results = []
for i,character in enumerate(s):
if not (i+1) % length:
results.append(tmp_string)
else:
tmp_string += character

I don't like this approach as you create to many temp items and fairly
ugly.
What you could do is to rather use slicing to build it.

results = []
length = 3
for i in xrange(0,len(s),length):
results.append(s[i:i+length])

And then the neatest approach would be to put that into a list
comprehension instead

s = "978654321"
step = 3
output = [s[start:start+step] for start in xrange(0,len(s),step)]

Those are just some ways to do it.
Hope that helps
 
J

John Machin

Hi,

I'm new to Python and I need to do the following:

from this: s = "978654321"
to this : ["978", "654", "321"]

Any help is appreciated

Homework?

Have you read the Python tutorial (section 3.1.2 Strings)?
 
E

ericdaniel

Hi Chris,

Thank you very much, that was exactly what I needed.

Eric

I'm new to Python and I need to do the following:
from this:   s = "978654321"
to this :      ["978", "654", "321"]
Any help is appreciated

Eric

What you could do is iterate over the string appending the characters
1 at a time to a new string and when you hit the point you want to
place it in your output list you can append it there and clear the
temp string eg.

length = 3
tmp_string = ''
results = []
for i,character in enumerate(s):
    if not (i+1) % length:
        results.append(tmp_string)
    else:
        tmp_string += character

I don't like this approach as you create to many temp items and fairly
ugly.
What you could do is to rather use slicing to build it.

results = []
length = 3
for i in xrange(0,len(s),length):
    results.append(s[i:i+length])

And then the neatest approach would be to put that into a list
comprehension instead

s = "978654321"
step = 3
output = [s[start:start+step] for start in xrange(0,len(s),step)]

Those are just some ways to do it.
Hope that helps
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top