简体   繁体   English

修改多级嵌套列表以制作单级列表

[英]Modify a multi-level nested list to make a single-level list

If I have a list formed by the command dic.keys() like this: 如果我有一个由命令dic.keys()组成的列表,如下所示:

[
  "['my', 'modem']", "['technical', 'schematics']", "['still', 'glad']",
  "['spent', 'calling']", "['most', 'feared']", "['the', 'sysop']",
  "['s', 'mystique']", "['had', 'been']", "['of', 'doom']",
  "['i', 'hooked']", "['in', 'california']", "['my', 'ego']",
  "['until', 'my']", "['didn', 't']"
]

The keys are formed by str() ,so it looks like list, but is a string. 这些键由str() ,因此它看起来像list,但是是一个字符串。 My question is, how can you separate each item to make it look like this? 我的问题是,如何分隔每个项目使其看起来像这样?

[ 'my', 'modem', 'techenical', ... ]

Just do a pattern matching for each string: 只需对每个字符串进行模式匹配:

res = []
for item in l:
    m = re.match("\['(.*)', '(.*)'\]", item)
    res.append(m.group(1))
    res.append(m.group(2))
#>>> res - ['my', 'modem', 'technical', ...]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM