【问题标题】:Get a certain string from the first elements - array从第一个元素中获取某个字符串 - 数组
【发布时间】:2021-04-07 18:51:38
【问题描述】:

给定一个数组: arr = ['Get your face right in there.', 'You’re like, wait, though, isn’t that too close? PUH-LEEZ. You and I both know that a person can never be too close to those browned lasagna edges that are the perfect amount of chewy, and that’s exactly why we like to hang out together and get wild about all the foods. That’s this lasagna florentine.']

我想从数组中获取一个字符串,其中包含总共 15 个或更多单词的元素。因此,由于第一个元素没有 15 个单词,输出将转到下一个元素,并将输出整个数组(在这种情况下)。前任。如果第一个元素有 12 个单词,第二个元素有 20 个单词,则输出字符串将有 12+20 = 32 个单词。

这是我的代码:

def CleanText(text):
    text = str(text)
    forbidden = [r'\n', r'\t', r'.', r'?', r'!', r'(', r')', r'/', r'<', r'>', '\\', '\\r', '</div>', '</a>']
    for i in forbidden:
        text.replace(i, '')
    return text

def ExtractText(arr):
    for i in arr:
        #return i
        if len(i) >= 15:
            return CleanText(i)
        elif len(arr[i]) < 15:
            attachedString = CleanText(arr[i+1])
            return CleanText(arr[i]) + attachedString

arr = ['Get your face right in there.', 'You&#8217;re like, wait, though, isn&#8217;t that too close? PUH-LEEZ. You and I both know that a person can never be too close to those browned lasagna edges that are the perfect amount of chewy, and that&#8217;s exactly why we like to hang out together and get wild about all the foods. That&#8217;s this lasagna florentine.']
print(ExtractFirstPara(arr))

输出:Get your face right in there. 但是,当我不概括它时,它就可以了。也就是说,当我明确地说:

def ExtractText(arr):
    for i in arr[0]:
        if len(i) >= 15:
            return CleanText(arr[0])
        else:
            attachedString = CleanText(arr[1])
            return CleanText(arr[0]) + attachedString

输出:

Get your face right in there.You&#8217;re like, wait, though, isn&#8217;t that too close? PUH-LEEZ. You and I both know that a person can never be too close to those browned lasagna edges that are the perfect amount of chewy, and that&#8217;s exactly why we like to hang out together and get wild about all the foods. That&#8217;s this lasagna florentine.

但是,在某些情况下,如果字符串与前两个元素的长度加起来不等于 15,那么这种显式性(如果它甚至是一个词)是不可能的。 为此,我需要概括一下。

【问题讨论】:

    标签: python arrays string loops


    【解决方案1】:

    如果我理解正确,没有部分元素输出:如果数组的第一个 k 元素的总字数至少为 nmin=15,则返回。

    如果这是正确的,并且您对找到与 r'(\w+)' 匹配的单词感到满意,那么以下操作会:

    def get_elements(arr, nmin = 15):
        p = re.compile(r'(\w+)')
        words_per_element = [len(p.findall(s)) for s in arr]
        ntot = 0
        for i, n in enumerate(words_per_element):
            ntot += n
            if ntot >= nmin:
                return arr[:i+1]
        return []
    

    测试:

    >>> get_elements(['a'], 1)
    ['a']
    
    >>> get_elements(['a'], 2)
    []
    
    >>> get_elements(['a', 'b c'], 1)
    ['a']
    
    >>> get_elements(['a', 'b c'], 2)
    ['a', 'b c']
    
    >>> get_elements(['a', 'b c'], 4)
    []
    
    >>> get_elements(['a', 'b c', 'd e f'], 2)
    ['a', 'b c']
    

    【讨论】:

      【解决方案2】:

      在这里试试这个:

      def CleanText(text):
          text = str(text)
          forbidden = [r'\n', r'\t', r'.', r'?', r'!', r'(', r')', r'/', r'<', r'>', '\\', '\\r', '</div>', '</a>']
          for i in forbidden:
              text.replace(i, '')
          return text
          
      def ExtractText(arr):
          string = ''
          for i in arr:
              string += i
              if len(string.split()) >= 15:
                  return string
      

      【讨论】:

        猜你喜欢
        • 2018-12-31
        • 1970-01-01
        • 1970-01-01
        • 2013-06-21
        • 1970-01-01
        • 1970-01-01
        • 2020-04-30
        • 2019-03-26
        • 2014-02-27
        相关资源
        最近更新 更多