473,320 Members | 1,854 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Create list from string

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
Jun 27 '08 #1
5 3663
On Jun 13, 3:15*pm, ericdaniel <eric.acev...@gmail.comwrote:
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.

--
Ben Sizer
Jun 27 '08 #2
On Jun 13, 4:15*pm, ericdaniel <eric.acev...@gmail.comwrote:
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
Jun 27 '08 #3
ericdaniel wrote:
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])
....
>>items
['978', '654', '321']

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

Peter

Jun 27 '08 #4
On Jun 14, 12:15 am, ericdaniel <eric.acev...@gmail.comwrote:
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)?
Jun 27 '08 #5
Hi Chris,

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

Eric

On Jun 13, 9:29*am, Chris <cwi...@gmail.comwrote:
On Jun 13, 4:15*pm, ericdaniel <eric.acev...@gmail.comwrote:
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
Jun 27 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: dog | last post by:
I've seen plenty of articles on this topic but none of them have been able to solve my problem. I am working with an Access 97 database on an NT4.0 machine, which has many Access reports. I...
1
by: Stephane | last post by:
Hi, I'm trying to create a virtual directory dynamically in my web site using ASP.NET. I received this error: System.Runtime.InteropServices.COMException (0x800700B7): Cannot create a file...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
19
by: Brett Romero | last post by:
Here's a table of data I'm putting into a collection: CodeId CodeGroup CodeSubGroup Type 1 K K.1 Shar1 2 K ...
23
by: sandy | last post by:
I need (okay, I want) to make a dynamic array of my class 'Directory', within my class Directory (Can you already smell disaster?) Each Directory can have subdirectories so I thought to put these...
4
by: etuncer | last post by:
Hello All, I have Access 2003, and am trying to build a database for my small company. I want to be able to create a word document based on the data entered through a form. the real question is...
1
by: Øyvind Isaksen | last post by:
I try to make my own ArticleAttribute object and ArticleAttributeCollection, and add data to this Collection. It almost works, but the problem is that each time I add an ArticleAttribute to my...
4
by: Vlad | last post by:
I am having problems using the file.create method within a function that is called when looping through an array of filepaths. If I call my function with a hardcoded file path --C:\Temp.txt the...
1
by: yaarnick | last post by:
Create a linked list data structure library to hold strings. Then library should support the following linked list operations. Create() – Creates the linked list Insert() – Insert a string into the...
2
by: Assimalyst | last post by:
Hi I have a Dictionary<string, List<string>>, which i have successfully filled. My problem is I need to create a filter expression using all possible permutations of its contents. i.e. the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.