【问题标题】:Extracting multiple substring from a string从字符串中提取多个子字符串
【发布时间】:2013-06-12 03:57:29
【问题描述】:

我有一个复杂的字符串,想尝试从中提取多个子字符串。

字符串由一组项目组成,以逗号分隔。每个项目都有一个标识符(id-n),用于在括号中括起来的一对单词。我只想得到括号内的单词,它的末尾附有一个数字(例如'This-1')。数字实际上表示提取后单词应该如何排列的位置。

#Example of how the individual items would look like
id1(attr1, is-2) #The number 2 here indicates word 'is' should be in position 2
id2(attr2, This-1) #The number 1 here indicates word 'This' should be in position 1
id3(attr3, an-3) #The number 3 here indicates word 'an' should be in position 3
id4(attr4, example-4) #The number 4 here indicates word 'example' should be in position 4
id5(attr5, example-4) #This is a duplicate of the word 'example'

#Example of string - this is how the string with the items looks like
string = "id1(attr1, is-1), id2(attr2, This-2), id3(attr3, an-3), id4(attr4, example-4), id5(atttr5, example-4)"

#This is how the result should look after extraction
result = 'This is an example'

有没有更简单的方法来做到这一点?正则表达式对我不起作用。

【问题讨论】:

  • 我无法理解您的示例。你能尝试用不同的方式来描述它吗?
  • @DaoWen - 抱歉,字符串本身有点复杂。很难形容。
  • 什么控制着项目的重新排序?
  • @IgnacioVazquez-Abrams - 括号中第二个单词末尾的数字。
  • 另一个例子发生了什么?

标签: python regex replace


【解决方案1】:

一种简单/幼稚的方法:

>>> z = [x.split(',')[1].strip().strip(')') for x in s.split('),')]
>>> d = defaultdict(list)
>>> for i in z:
...    b = i.split('-')
...    d[b[1]].append(b[0])
...
>>> ' '.join(' '.join(d[t]) for t in sorted(d.keys(), key=int))
'is This an example example'

您的示例字符串中有重复的example 位置,这就是代码中重复example 的原因。

但是,您的样本也不符合您的要求 - 但此结果与您的描述相符。单词按其位置指示符排列。

现在,如果您想删除重复项:

>>> ' '.join(e for t in sorted(d.keys(), key=int) for e in set(d[t]))
'is This an example'

【讨论】:

  • example 被重复,但是,这不是 OP 想要的。
  • 好吧,如果您想成为技术人员,那么他的示例实际上也不是他想要的,因为单词被互换了。
  • 我认为这是一个错字。但是对于同一个职位的要求已经说得很清楚了。
  • @BurhanKhalid - 这正是我所需要的。非常感谢!
【解决方案2】:

为什么不用正则表达式?这行得通。

In [44]: s = "id1(attr1, is-2), id2(attr2, This-1), id3(attr3, an-3), id4(attr4, example-4), id5(atttr5, example-4)"

In [45]: z = [(m.group(2), m.group(1)) for m in re.finditer(r'(\w+)-(\d+)\)', s)]

In [46]: [x for y, x in sorted(set(z))]
Out[46]: ['This', 'is', 'an', 'example']

【讨论】:

  • 您未能检测到example 被重复(4 的位置相同),应该只保留其中一个。
  • 好的,我已经修改了从 Burahn 的回答中借用 set
【解决方案3】:

好的,这个怎么样:

sample = "id1(attr1, is-2), id2(attr2, This-1), 
          id3(attr3, an-3), id4(attr4, example-4), id5(atttr5, example-4)"


def make_cryssie_happy(s):
    words = {} # we will use this dict later
    ll = s.split(',')[1::2]
    # we only want items like This-1, an-3, etc.

    for item in ll:
        tt = item.replace(')','').lstrip()
        (word, pos) = tt.split('-')
        words[pos] = word
        # there can only be one word at a particular position
        # using a dict with the numbers as positions keys 
        # is an alternative to using sets

    res = [words[i] for i in sorted(words)]
    # sort the keys, dicts are unsorted!
    # create a list of the values of the dict in sorted order

    return ' '.join(res)
    # return a nice string


print make_cryssie_happy(sample)

【讨论】:

    猜你喜欢
    • 2019-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-09
    • 2011-07-21
    相关资源
    最近更新 更多