3

I have a string that I inserted a space into it in all different positions and saved them to a list. Now this list of strings with space in them, I want to split those strings and put the output in one list, when am doing this, it happens that am having multiple list inside:

This is the code am working on:

var ='sans'
res = [var[:i]+' '+var[i:] for i in range(len(var))]
// The previous line: AM adding a space to see maybe that would generate other words
cor = [res[i].split() for i in range (len(res))]

And this is the output am getting:

>>> cor
[['sans'], ['s', 'ans'], ['sa', 'ns'], ['san', 's']]

What am expecting:

>>> cor
    ['sans', 's', 'ans', 'sa', 'ns', 'san', 's']

Am new to python, I don't know what am missing.

Thanks

5 Answers 5

6

An alternative approach:

cor = " ".join(res).split()

Output:

['sans', 's', 'ans', 'sa', 'ns', 'san', 's']

Explanation

" ".join(res) will join the individual strings in res with a space in between them. Then calling .split() will split this string on whitespace back into a list.

EDIT: A second approach that doesn't involve the intermediate variable res, although this one isn't quite as easy on the eyes:

cor = [var[:i/2+1] if i%2==1 else var[i/2:] for i in range(2*len(var)-1)]

Basically you flip between building substrings from the front and the back.

Sign up to request clarification or add additional context in comments.

19 Comments

@roganjosh Compared to yours maybe :-P
@StefanPochmann haha, oh come on, this is gonna have to come down to a timeit because this answer beats even a list comp IMO in simplicity :P
@StefanPochmann's answer is likely the best (he's got my upvote) but double list comprehensions can be difficult to understand for someone thats "new to python."
@roganjosh But it's much faster than ours.
@roganjosh Yeah you made a big error there. I'm not spelled with "ph" :-). Also, there's no timing code, it's just the solutions.
|
3

First of all, your

[res[i].split() for i in range (len(res))]

is a complicated unpythonic way to do the same as this:

[r.split() for r in res]

Now... the problem is that you treat r.split() as your end result. You should instead use it as a source to treat it further:

[s for r in res for s in r.split()]

2 Comments

This approach seems to be quite fast also, as seen here.
@RoadRunner With var ='sans' * 1000 and n = 40 it looks better: ideone.com/6dC8Rb. But interestingly, Dekel's is even faster then. That didn't seem right, so I tested them locally independently (also with Python 3.5) and mine was faster there. Then I moved mine to the end and lo and behold, it became the fastest: ideone.com/yYXRrn. Then I moved yours to the end and it became almost the fastest: ideone.com/sN5Gnn. I guess I'll have to stop benchmarking on ideone. Seems like later tests benefit from earlier tests increasing the process priority or so.
2

If you have a list

cor = [['sans'], ['s', 'ans'], ['sa', 'ns'], ['san', 's']]

And you want to flatten it, you can use the following:

flat = [x for y in cor for x in y]

The output will be:

['sans', 's', 'ans', 'sa', 'ns', 'san', 's']

You can also make that directly with the res variable:

cor = [x for y in [res[i].split() for i in range (len(res))] for x in y]

3 Comments

Would be better to not build that cor in the first place.
@StefanPochmann you are right, added an example for that as well
Nah, that's not what I meant. You're still building that list (even in the exact same bad way).
1

You coud always use map() to split each string in res:

list(map(str.split, res))

Which gives:

[['sans'], ['s', 'ans'], ['sa', 'ns'], ['san', 's']]

Then you can use itertools.chain.from_iterable to flatten the list:

list(chain.from_iterable(map(str.split, res)))

Which Outputs:

['sans', 's', 'ans', 'sa', 'ns', 'san', 's']

Comments

0

You could do something like this in one line without importing any module :

var ='sans'

final=[]
list(map(lambda x:list(map(lambda y:final.append(y),x)),[(var[i:]+' '+var[:i]).split() for i in range(0,len(var))]))
print(final)

output:

['sans', 'ans', 's', 'ns', 'sa', 's', 'san']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.