简体   繁体   中英

Avoid an operation on the first and/or last element

I have a list with some text:

my_list = ['sentence1','sentence2','sentence3','sentence4' ]

And I want as output a string like this:

'sentence1, sentence2, sentence3, sentence4'

If I make this, I get an aditional ', ' at the end

string_sentences = ''
for sentence in my_list:
    string_sentences +=  sentence +', '
string_sentences

output: 'sentence1, sentence2, sentence3, sentence4, '

same with this approach, but now at the beginning:

string_sentences = ''
for sentence in my_list:
    string_sentences += ', ' + sentence 
string_sentences

', sentence1, sentence2, sentence3, sentence4'

Only thing I can think to solve it is to keep track of the indices

string_sentences = ''
for i,sentence in enumerate(my_list):
    if i +1 < len(my_list):
        string_sentences += sentence +', '
    else:
        string_sentences += sentence
string_sentences

ouput: 'sentence1, sentence2, sentence3, sentence4'

But this seems like a very common issues, and I wonder whether is a more pythonic way to solve it.

Use the join method to convert your list to a string. Provide a separator that will go between each element that is extracted from the list that goes in to your string. In this case , and a space ' ' after the comma:

my_list = ['sentence1','sentence2','sentence3','sentence4' ]
s = ', '.join(my_list)

By printing s we will have:

'sentence1, sentence2, sentence3, sentence4'

Just some extra notes about why using join here would actually be preferable, is because it actually runs at C speed as opposed to the speed you get from a Python loop. Read about performance tips here .

Finally, a warning when doing this. The join always expects an iterable of strings. Which means, if you have a list of integers [1, 2, 3, 4] . You will not be able to call join directly on this. You would have to cast each of those values to an str . A simple example would be something like this:

list_of_ints = [1, 2, 3, 4]
', '.join([str(n) for n in list_of_ints])

So, to further explain what just happened in the above example. Since we actually have a list of integers. In order to successfully create our string from that list, we need to cast each element in the list to a string. In order to do this, we will use a list-comprehension to create this list of string integers, and then call your join method.

Now, the reason why we are actually not using a generator expression here and using a list comprehension, is because in fact the join method will actually scan the iterable twice, therefore the comprehension in this case is actually faster.

This is actually very nicely explained here .

Is this what you want?

>>> my_list = ['sentence1','sentence2','sentence3','sentence4' ]
>>> ', '.join(my_list)
'sentence1, sentence2, sentence3, sentence4'
>>> 
>>> result = ', '.join(my_list)
>>> result
'sentence1, sentence2, sentence3, sentence4'
>>> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM