2020年12月2日水曜日

Python :split関数の使い方

 str = 'Today is the anniversary of the publication of Robert Frost’s iconic poem “Stopping by Woods on a Snowy Evening,” a fact that spurred the Literary Hub office into a long conversation about their favorite poems, the most iconic poems written in English, and which poems we should all have already read (or at least be reading next). Turns out, despite frequent (false) claims that poetry is dead and/or irrelevant and/or boring, there are plenty of poems that have sunk deep into our collective consciousness as cultural icons. (What makes a poem iconic? For our purposes here, it’s primarily a matter of cultural ubiquity, though unimpeachable excellence helps any case.) So for those of you who were not present for our epic office argument, I have listed some of them here.'

#単語に分割する
splits = str.split()

print(splits)

print(len(str))#文章の文字数を表示する。
print(len(splits))#文章の単語数を表示する。

出力

['Today', 'is', 'the', 'anniversary', 'of', 'the', 'publication', 'of', 'Robert', 'Frost’s', 'iconic', 'poem', '“Stopping', 'by', 'Woods', 'on', 'a', 'Snowy', 'Evening,”', 'a', 'fact', 'that', 'spurred', 'the', 'Literary', 'Hub', 'office', 'into', 'a', 'long', 'conversation', 'about', 'their', 'favorite', 'poems,', 'the', 'most', 'iconic', 'poems', 'written', 'in', 'English,', 'and', 'which', 'poems', 'we', 'should', 'all', 'have', 'already', 'read', '(or', 'at', 'least', 'be', 'reading', 'next).', 'Turns', 'out,', 'despite', 'frequent', '(false)', 'claims', 'that', 'poetry', 'is', 'dead', 'and/or', 'irrelevant', 'and/or', 'boring,', 'there', 'are', 'plenty', 'of', 'poems', 'that', 'have', 'sunk', 'deep', 'into', 'our', 'collective', 'consciousness', 'as', 'cultural', 'icons.', '(What', 'makes', 'a', 'poem', 'iconic?', 'For', 'our', 'purposes', 'here,', 'it’s', 'primarily', 'a', 'matter', 'of', 'cultural', 'ubiquity,', 'though', 'unimpeachable', 'excellence', 'helps', 'any', 'case.)', 'So', 'for', 'those', 'of', 'you', 'who', 'were', 'not', 'present', 'for', 'our', 'epic', 'office', 'argument,', 'I', 'have', 'listed', 'some', 'of', 'them', 'here.']
775
130

 

参考リンク

文字列を分割する!Pythonでsplit関数を使う方法を現役エンジニアが解説【初心者向け】 

Pythonのsplitで文字列を分割する方法

0 件のコメント:

コメントを投稿