【问题标题】:Python: How to make multiple string sublists from sentence elements in list?Python:如何从列表中的句子元素制作多个字符串子列表?
【发布时间】:2015-01-04 11:44:47
【问题描述】:

所以这是我的第一个列表:

listone = [
           'Every day Miss Leeson went out to work', 
           ' At night she brought home papers with handwriting on them and made copies with her typewriter', 
           ' Sometimes she had no work at night, and then she would sit on the steps of the high stoop with the other roomers', 
           ' Miss Leeson was not intended for a sky-light room when the plans were drawn for her creation', 
           ' She was gay-hearted and full of tender, whimsical fancies', 
           ' Once she let Mr', 
           ' Skidder read to her three acts of his great (unpublished) comedy, "It'
          ]

在这个列表中,每个句子都是一个元素,我想让每个句子成为一个子列表,或者只有句子的一部分成为一个子列表,例如:

listtwo = [['Every','Miss','work'], ['At', 'she', 'with'] etc.]

子列表中的单词是随机选择的,我已经有一个过滤功能可以使用。

感谢您的帮助!

【问题讨论】:

  • 它将在每个子列表中以三个为一组??总是来自原始列表
  • [s.split() for s in listone]

标签: python string list for-loop


【解决方案1】:

你也可以使用map函数:

>>> map(str.split, listone)
[['Every', 'day', 'Miss', 'Leeson', 'went', 'out', 'to', 'work'], ['At', 'night', 'she', 'brought', 'home', 'papers', 'with', 'handwriting', 'on', 'them', 'and', 'made', 'copies', 'with', 'her', 'typewriter'], ['Sometimes', 'she', 'had', 'no', 'work', 'at', 'night,', 'and', 'then', 'she', 'would', 'sit', 'on', 'the', 'steps', 'of', 'the', 'high', 'stoop', 'with', 'the', 'other', 'roomers'], ['Miss', 'Leeson', 'was', 'not', 'intended', 'for', 'a', 'sky-light', 'room', 'when', 'the', 'plans', 'were', 'drawn', 'for', 'her', 'creation'], ['She', 'was', 'gay-hearted', 'and', 'full', 'of', 'tender,', 'whimsical', 'fancies'], ['Once', 'she', 'let', 'Mr'], ['Skidder', 'read', 'to', 'her', 'three', 'acts', 'of', 'his', 'great', '(unpublished)', 'comedy,', '"It']]

并将其与您的过滤函数链接(在这种情况下,选择 3 个随机单词)

>>> import random
>>> map(lambda x: random.sample(x, 3), map(str.split, listone))
[['Every','Miss','work'], ['At', 'she', 'with'] etc.]

(或者,使用列表理解:)

>>> [random.sample(x.split(), 3) for x in listone]

【讨论】:

    【解决方案2】:

    您可以使用split 的列表推导来分解句子

    >>> [i.split() for i in listone]
    [['Every', 'day', 'Miss', 'Leeson', 'went', 'out', 'to', 'work'],
     ['At', 'night', 'she', 'brought', 'home', 'papers', 'with', 'handwriting', 'on', 'them', 'and', 'made', 'copies', 'with', 'her', 'typewriter'],
     ['Sometimes', 'she', 'had', 'no', 'work', 'at', 'night,', 'and', 'then', 'she', 'would', 'sit', 'on', 'the', 'steps', 'of', 'the', 'high', 'stoop', 'with', 'the', 'other', 'roomers'],
     ['Miss', 'Leeson', 'was', 'not', 'intended', 'for', 'a', 'sky-light', 'room', 'when', 'the', 'plans', 'were', 'drawn', 'for', 'her', 'creation'],
     ['She', 'was', 'gay-hearted', 'and', 'full', 'of', 'tender,', 'whimsical', 'fancies'],
     ['Once', 'she', 'let', 'Mr'],
     ['Skidder', 'read', 'to', 'her', 'three', 'acts', 'of', 'his', 'great', '(unpublished)', 'comedy,', '"It']]
    

    您还可以在列表推导中使用filter 来应用您的过滤功能来删除不需要的单词。

    【讨论】:

    • 我认为 OP 不想要这样
    • 是什么让你这么想?
    • 我认为从每个句子中 OP 想要子列表中的三个随机单词
    • 不,他们在示例中随机选择了单词。他们说他们有过滤功能来选择单词。所以他们可以为此使用mapfilter
    • 感谢您的快速响应,我现在感觉很笨,但很高兴。
    【解决方案3】:

    如果你想从每个句子中随机抽取 3 个:

     def get_random(a):
         return random.sample(a,3)
     [ get_random(x.split()) for x in listone ]
     '''or using map function'''
     map(get_random,map(str.split,listone))
    

    输出:

    [['to', 'went', 'out'], ['on', 'At', 'with'], ['she', 'and', 'would'], ['her', 'room', 'were'], ['tender,', 'was', 'full'], ['Mr', 'let', 'Once'], ['Skidder', 'her', '(unpublished)']]
    

    【讨论】:

    • 它的random.randint,它可以再次选择相同的整数
    猜你喜欢
    • 2014-03-10
    • 2020-04-05
    • 2019-08-03
    • 1970-01-01
    • 2015-02-15
    • 1970-01-01
    • 2020-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多