简体   繁体   English

如何为每个拆分执行命令?

[英]How do I excecute a command for every split?

I am new to python, and am trying to filter a string that looks similar to this: 我是python的新手,正在尝试过滤类似于以下内容的字符串:

"{Red,Plant,Eel}{Blue,Animal,Maple}{Yellow,Plant,Crab}"

And so on for 100s of three word sets. 依此类推,持续三个单词集100个。

I want to extract the second word from every set marked by "{ }", so in this example I want the output: 我想从每个用“ {}”标记的集合中提取第二个单词,因此在此示例中,我需要输出:

"Plant,Animal,Plant"

And so on. 等等。

How can I do it efficiently? 我如何有效地做到这一点?

As of Right now I am using string.split(",")[1] individually for each "{ }" group. 截至目前,我为每个“ {}”组分别使用string.split(",")[1]

Thanks. 谢谢。

This does the trick: 这可以解决问题:

str_ = "{Red,Plant,Eel}{Blue,Animal,Maple}{Yellow,Plant,Crab}"
res = [x.split(',')[1] for x in str_[1:-1].split('}{')]

and produces 并产生

['Plant', 'Animal', 'Plant']

with the str_[1:-1] we remove the initial "{" and trailing "}" and we then split the remaining entities on every instance of "}{" thus producing: 使用str_[1:-1]删除str_[1:-1]"{"和结尾的"}" ,然后在"}{"每个实例上拆分其余实体,从而产生:

["Red,Plant,Eel", "Blue,Animal,Maple", ...]

finally, for every string, we split on "," to obtain 最后,对于每个字符串,我们在","上拆分以获得

[["Red", "Plant", "Eel"], ...]

from which we keep only the first element of each sublist with x[1] . 从中,我们仅使用x[1]保留每个子列表的第一个元素。

Note that for your specific purpose, slicing the original string with str_[1:-1] is not mandatory (works without it as well), but if you wanted only the first instead of the second item it would make a difference. 请注意,出于您的特定目的,使用str_[1:-1]分割原始字符串不是强制性的(也可以不使用它),但是如果您只希望第一项而不是第二项,那将会有所不同。 The same holds in case you wanted the 3rd. 如果您想获得第三名,也是如此。


If you want to concatenate the strings of the output to match your desired result, you can simply pass the resulting list to .join as follows: 如果要连接输出的字符串以匹配所需的结果,只需将结果列表传递给.join ,如下所示:

out = ','.join(res)

which then gives you 然后给你

"Plant,Animal,Plant"

尝试这个:

[i.split(',')[1] for i in str_[1:].split('}')[:len(str_.split('}'))-1]]

another solution is using regex, a bit more complicated, but it's a technique worth talking about: 另一个解决方案是使用正则表达式,它稍微复杂一些,但这是一种值得讨论的技术:

import re
input_string = "{Red,Plant,Eel}{Blue,Animal,Maple}{Yellow,Plant,Crab}"
regex_string = "\{\w+\,(\w+)\,\w+\}"

result_list = re.findall(regex, input_string)

then result_list output is: 那么result_list输出是:

['Plant', 'Animal', 'Plant'] [“植物”,“动物”,“植物”]

here's a link for regex in python and an online regex editor 这是python中的正则表达式的链接在线正则表达式编辑器

#!/bin/python3

string = "{Red,Plant,Eel}{Blue,Animal,Maple}{Yellow,Plant,Crab}"
a = string.replace('{','').replace('}',',').split(',')[1::3]
print(a)

result is ['Plant', 'Animal', 'Plant'] 结果是['Plant', 'Animal', 'Plant']

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

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