0
複数の行からなる関数を1行だけの関数に変換しようとしています。"複数行"関数を "1行"に変換する - 関数
複数行の関数は次のようになります。
text = “Here is a tiny example.”
def add_text_to_list(text):
new_list = []
split_text = text.splitlines() #split words in text and change type from “str” to “list”
for line in split_text:
cleared_line = line.strip() #each line of split_text is getting stripped
if cleared_line:
new_list.append(cleared_line)
return new_list
私は100%で、この機能がどのように働くか、それが何をするかを理解し、まだ私は悩みに有効な「oneliner」にこれを実装しています。私はまた、私はリストの理解を思い付く必要があることを知っています。私は何をしようとしていること(年代順に)これです:私は助けのいずれかの種類を感謝
def one_line_version(text):
return [line.strip() for line in text.splitlines()] #step 1 is missing
:
1. split words of text with text.splitlines()
2. strip lines of text.splitlines with line.strip()
3. return modified text after both of these steps
最高の私が思い付きました。
編集:Thanks @Tenfrow!
の神、私のオハイオ州では約
if
を忘れてしまった... – Miggl私の解決策はあったが、「行であれば」とそれは同様に足ります – Miggl