ezdazuzena
ezdazuzena

Reputation: 6770

create list of substrings of several lines

Imagine I have the following list:

result_lines = ['name1 age1 address1 email1',
                'name2 age2 address2 email2',
                'name3 age3 address3 email3']

I would like to print the following string:

'age1:name1, age2:name2, age3: name3'

Note: no , at the end of the string!

UPDATE: important is not the order age1:name1. it could also be age1:email1:name1.

What I tried so far:

print "".join((l.split(' ')[1], l.split(' ')[0]) for l in result_lines) 

However, I get the following error message:

TypeError: sequence item 0: expected string, tuple found

Thanks for any help.

Upvotes: 2

Views: 114

Answers (3)

Martijn Pieters
Martijn Pieters

Reputation: 1121654

To do this on one line without multiple calls to split you need to reverse and slice the .split() result, and nest your string joins:

', '.join([':'.join(line.split()[:2][::-1]) for line in result_lines])

So each entry in the list is:

  • Split on whitespace
  • Sliced to only give the name and age entries
  • Reversed so age comes before name using [::-1] to reverse in-place.
  • Joined with ':' to give the age?:name? pairs.
  • We use a list comprehension ([ ... for .. in ... ]) because that's faster with str.join(). Internally, .join() calculates the final string length before joining and will cast a generator to a list anyway. The comprehension then wins on speed.

Then the whole list is joined with ', ' to give a nice comma-separated list. Because we only split once this will run

This gives:

>>> ', '.join([':'.join(line.split()[:2][::-1]) for line in result_lines])
'age1:name1, age2:name2, age3:name3'

Upvotes: 4

johnsyweb
johnsyweb

Reputation: 141790

str.format() is your friend in a situation where you want to present the contents of a list or tuple:

>>> ', '.join(['{1}:{0}'.format(*line.split()) for line in result_lines])
'age1:name1, age2:name2, age3:name3'

To break this down a little, because there's a lot going on in that line...

We start with the simple list comprehension:

>>> [line for line in result_lines]
['name1 age1 address1 email1', 'name2 age2 address2 email2', 'name3 age3 address3 email3']

And split the string into whitespace using str.split() (we only need to split once):

>>> [line.split() for line in result_lines]
[['name1', 'age1', 'address1', 'email1'], ['name2', 'age2', 'address2', 'email2'], ['name3', 'age3', 'address3', 'email3']]

Introduce str.format() and unpack the argument list:

>>> ['{0}:{1}:{2}:{3}'.format(*line.split()) for line in result_lines]
['name1:age1:address1:email1', 'name2:age2:address2:email2', 'name3:age3:address3:email3']

Choose the elements we want:

>>> ['{1}:{0}'.format(*line.split()) for line in result_lines]
['age1:name1', 'age2:name2', 'age3:name3']

(str.)join it all together with ', ':

>>> ', '.join(['{1}:{0}'.format(*line.split()) for line in result_lines])
'age1:name1, age2:name2, age3:name3'

Q.E.D.

Upvotes: 6

Timo
Timo

Reputation: 5390

This should do the work:

>>> result_lines = ['name1 age1 address1 email1', \
...                 'name2 age2 address2 email2', \
...                 'name3 age3 address3 email3']
>>> print ", ".join([l.split(' ')[1] + ': ' + l.split(' ')[0] for l in result_lines])
age1: name1, age2: name2, age3: name3

Upvotes: 1

Related Questions