468,539 Members | 1,856 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,539 developers. It's quick & easy.

How to convert a string into list of strings

Hello,
I am trying to convert a string into list of strings.
The output is like the following:
[['a', 'a'], ['b'], ['c']].

However, the output must be like:
['a', 'a', 'b', 'c']

My code is like the following:

Expand|Select|Wrap|Line Numbers
  1. return_letter = ""
  2. letter_list = []
  3. f = open(file_name,"r")
  4. for line in f:
  5.         k = line[:1]
  6.         letter_list.append(k.split()*int(line[1:]))
  7. print letter_list  
The file which is being read looks like this:
a2
b1
c1

Thank you in advance.
Apr 25 '10 #1

✓ answered by bvdet

Please use code tags when posting code. See posting guidelines here.

Try this:
Expand|Select|Wrap|Line Numbers
  1.         for i in range(int(line[1:])
  2.             letter_list.append(line[:1])
BV - Moderator

2 1512
bvdet
2,851 Expert Mod 2GB
Please use code tags when posting code. See posting guidelines here.

Try this:
Expand|Select|Wrap|Line Numbers
  1.         for i in range(int(line[1:])
  2.             letter_list.append(line[:1])
BV - Moderator
Apr 26 '10 #2
@bvdet
Thank you very much :))) It's working!!!!!!!
Apr 26 '10 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by flyaflya | last post: by
7 posts views Thread by Philipp H. Mohr | last post: by
13 posts views Thread by Hako | last post: by
13 posts views Thread by HNT20 | last post: by
4 posts views Thread by csharpula csharp | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.