我有一个字符串数组。我想打印以New开头的那些。

时间:2018-02-21 18:11:42

标签: python

我有一个字符串数组。我想打印以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"
]

1 个答案:

答案 0 :(得分:0)

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字符串文档here