![](/img/trans.png)
[英]If there is list of 4 string elements. Several elements are - separated strings. If I want to print just one part of the - separated word
[英]I have an array of strings. I want to print those that start with the word New.
我有一個字符串數組。 我想打印以 New 開頭的那些。
input_arr = [
"New: Hello",
"How are",
"you",
"New: I am",
"fine"
]
def merge_messages(input_arr):
#don't know that to do here
return input_arr
print(merge_messages(input_arr))
#Expected Output
[
"New: Hello How are you",
"New: I am fine"
]
input_arr = [
"New: Hello",
"How are",
"you",
"New: I am",
"fine"
]
def merge_messages(input_arr):
delimiter = "New"
return [delimiter+x for x in " ".join(input_arr).split(delimiter) if x]
print(merge_messages(input_arr))
但是,我強烈建議您接受評論中給您的建議並閱讀 Python 字符串。 您還可以在此處查看 Python 字符串文檔
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.