【问题标题】:How to convert array of lists into a corpus of strings and the strings are multiplied by the number (example provided)如何将列表数组转换为字符串语料库并将字符串乘以数字(提供的示例)
【发布时间】:2021-05-22 17:46:06
【问题描述】:

我有以下列表数组。

[array(['I', '1', 'am', '1', 'a', '1', 'boy', '3']) array(['I', '1', 'am', 'a', 'girl', '1']) array(['I', '1', 'am', '1', 'non-binary', '2'])

我想编写一个 python 代码将数组转换为一个语料库,其中文本乘以数字,即这种格式:

corpus = [ "I am a boy boy boy", "I am a girl", "I am non-binary non-binary]

如何在 Python 上实现这一点?任何帮助将不胜感激。

【问题讨论】:

    标签: python string list repeat


    【解决方案1】:

    尝试以下方法:

    data = [['I', '1', 'am', '1', 'a', '1', 'boy', '3'], ['I', '1', 'am', '1', 'a', '1', 'girl', '1'], ['I', '1', 'am', '1', 'non-binary', '2']]
    
    def stringify(lst): # function: convert list into str with the words repeated
        repeated = [' '.join([lst[i]] * int(lst[i + 1])) for i in range(0, len(lst), 2)]
        return ' '.join(repeated)
    
    output = [stringify(x) for x in data] # apply the function for each item
    print(output) # ['I am a boy boy boy', 'I am a girl', 'I am non-binary non-binary']
    

    这个基本上是重复应用列表推导和join方法。

    【讨论】:

    • 这个解决方案对我有用。非常感谢您的回复。
    【解决方案2】:

    您可以使用列表解析来获取所有单词,然后使用另一个列表解析来“展平”列表。

    data = [['I', '1', 'am', '1', 'a', '1', 'boy', '3'], 
            ['I', '1', 'am', '1', 'a', '1', 'girl', '1'], 
            ['I', '1', 'am', '1', 'non-binary', '2']]
    
    corpus = [[word]*int(sentence[pos+1]) for sentence in data for pos, word in enumerate(sentence) if pos % 2 == 0]
    
    corpus = [word for words in corpus for word in words]
    
    print(corpus)
    
    """ OUTPUT
    ['I', 'am', 'a', 'boy', 'boy', 'boy', 'I', 'am', 'a', 'girl', 'I', 'am', 'non-binary', 'non-binary']
    """
    

    【讨论】:

      【解决方案3】:

      这是我想出的。

      def corpus(list):
          s = []
          for x in range(len(list)):
              if not list[x].isdigit():
                  if (x != len(list)-1) and (list[x+1].isdigit()):
                      for num in range(int(list[x+1])):
                          s.append(list[x])
                  else:
                      s.append(list[x])
          return ' '.join(s)
      

      这应该检查列表的每个索引后是否有一个数字,并返回索引乘以该数字并在其中包含必要的空格。抱歉,如果它很笨重,我试图快速完成。如果您需要澄清任何 if/else 语句,请告诉我。

      编辑:我花了一些额外的时间,这样代码就不会那么笨重。我们不是每次都在字符串中添加一个空格并针对最终添加进行调整,而是使用 ' '.join() 在每个条目之间添加空格。 将其应用于您提供的列表会产生

      a = [['I', '1', 'am', '1', 'a', '1', 'boy', '3'],
           ['I', '1', 'am', 'a', 'girl', '1'],
           ['I', '1', 'am', '1', 'non-binary', '2']]
      
      print(list(map(corpus,a)))
      
      ['I am a boy boy boy', 'I am a girl', 'I am non-binary non-binary']
      

      【讨论】:

        【解决方案4】:

        您的数据存在一些问题:

        1. array 未定义。
        2. 末尾有),但末尾没有(
        3. 外部列表中的元素之间没有任何逗号。
        4. 在中间列表中,“am”或“a”后面没有数字。
        5. 从风格的角度来看,列表之间应该有回车。

        综合起来,我有以下几点:

        my_list = [
            ['I', '1', 'am', '1', 'a', '1', 'boy', '3'],
            ['I', '1', 'am', '1', 'a', '1', 'girl', '1'],
            ['I', '1', 'am', '1', 'non-binary', '2']
         ]
        

        鉴于此输入,以下代码有效:

        [
            " ".join([" ".join([word]*int(count)) 
                      for word, count in zip(sublist[::2],sublist[1::2])])
            for sublist in my_list
        ]
        

        【讨论】:

          【解决方案5】:

          一个选项通过zip:

          l = [['I', '1', 'am', '1', 'a', '1', 'boy', '3'], ['I', '1', 'am', '1', 'a', '1', 'girl', '1'], ['I', '1', 'am', '1', 'non-binary', '2']]
          result = [' '.join((' '.join([x] * int(y))) for x,y in zip(i[::2], i[1::2])) for i in l]
          

          Output:

          ['I am a boy boy boy', 'I am a girl', 'I am non-binary non-binary']
          

          【讨论】:

          • @Accccumulation 添加了初始列表l 你可以运行,如果我错过了什么,请告诉我。
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-12-01
          • 1970-01-01
          • 1970-01-01
          • 2021-12-13
          • 1970-01-01
          • 1970-01-01
          • 2011-06-18
          相关资源
          最近更新 更多