How can I replace a text delimited string list item with multiple list items in a python list?

前端 未结 12 2193
遥遥无期
遥遥无期 2020-12-11 05:17

Given a list:

mylist = [\'dog\', \'cat\', \'mouse_bear\', \'lion_tiger_rabbit\', \'ant\']

I\'d like a one-liner to return a new list:

12条回答
  •  旧时难觅i
    2020-12-11 05:43

    Split each item into sublists and flatten them:

    [item for sublist in mylist for item in sublist.split("_")]

提交回复
热议问题