【问题标题】:How to reorganize a list in specific way in Python如何在 Python 中以特定方式重新组织列表
【发布时间】:2019-01-09 02:00:31
【问题描述】:

所以,如果你有以下列表,我想做的是:

example_list=['This', 'is', 'QQQQQ', 'an', 'QQQQQ', 'example', 'list', 'QQQQQ', '.']

我希望它被重新组织成这样:

example_list=['This is', 'an', 'example list', '.']

注意 QQQQQ 是如何被用作占位符的。所以,基本上我希望 QQQQQ 之间的所有内容都成为一个列表元素。我该怎么做?

我看过其他关于 join() 函数的帖子,但我遇到的问题是如果超过 1 个单词,则在两者之间放置一个空格。

【问题讨论】:

  • @dfundako 不能解决空间问题。

标签: python list list-manipulation


【解决方案1】:

使用简单的迭代。

例如:

example_list=['This', 'is', 'QQQQQ', 'an', 'QQQQQ', 'example', 'list', 'QQQQQ', '.']

res = [[]]
for i in example_list:
    if i == "QQQQQ":
        res.append([])
    else:
        res[-1].append(i)
print([" ".join(i) for i in res])

输出:

['This is', 'an', 'example list', '.']

【讨论】:

  • res[-1] 是干什么用的?
  • @Rakesh 真的没什么大不了,我只是想知道为什么要链接骗子?
  • 抱歉,我无法理解为什么 res[-1].append(i) 会这样工作。
  • 尝试:a = [1,2,3,4,5]print(a[-1]) 它应该打印 5
  • @scharette..抱歉没有注意到这是骗子
【解决方案2】:

你可以使用itertools.groupby():

>>> from itertools import groupby
>>> example_list=['This', 'is', 'QQQQQ', 'an', 'QQQQQ', 'example', 'list', 'QQQQQ', '.']
>>> [' '.join(g) for k, g in groupby(example_list, lambda x: x == 'QQQQQ') if not k]
['This is', 'an', 'example list', '.']

或者甚至与 .__eq__ 比较,正如 cmets 中的 @tobias_k 所建议的那样:

>>> [' '.join(g) for k, g in groupby(example_list, key='QQQQQ'.__eq__) if not k]
['This is', 'an', 'example list', '.']

【讨论】:

  • 你也可以使用key='QQQQQ'.__eq__(不过不确定是否更好)
  • @tobias_k 这是使用key 的好方法。更新的答案也包括在内:)。
【解决方案3】:

尝试将joinstrip() 一起消除空格

answer = [s.strip() for s in ' '.join(map(str, example_list)).split('QQQQQ')]
print (answer)

输出

['This is', 'an', 'example list', '.']

【讨论】:

    【解决方案4】:

    简单的解决方案:使用空格进行连接,然后将空格添加到拆分函数中的占位符。

    示例:

    example_list = ['This', 'is', 'QQQQQ', 'an', 'QQQQQ', 'example', 'list', 'QQQQQ', '.']
    
    print(' '.join(example_list).split(' QQQQQ '))
    

    结果:

    ['This is', 'an', 'example list', '.']
    

    或更广义的:

    split_arg = ' {} '.format(place_holder)
    example_list = ' '.join(example_list).split(split_arg)
    

    tobias_k 评论后编辑

    评论是:“当然,这仅在占位符实际上是一个字符串时才有效,并且如果该搅拌没有出现在任何其他单词中。也就是说,如果占位符是,例如,None,则它将不起作用,' Q',或''——tobias_k"

    这是真的,所以我提出了一个更通用的解决方案,因此它应该适用于每个占位符。

    import random
    import string
    
    example_list = ['This', 'is', None, 'an', None, 'example', 'list', None, '.']
    place_holder = None
    # create a random string of length 10
    random_place_holder = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10))  
    # Replace all old place holders with our new random string placeholder
    example_list = [x if x != place_holder else random_place_holder for x in example_list ]
    split_arg = ' {} '.format(random_place_holder)
    example_list = ' '.join(example_list).split(split_arg)
    print(example_list)
    

    老实说,如果您有一个不方便的占位符(例如 tobias_k 提到的),您最好使用任何其他解决方案。

    决定计时: 使用:

    example_list = ['This', 'is', None, 'an', None, 'example', 'list', None, '.'] * 10000
    place_holder = None
    

    我使用了一个更长的列表,这样随机字符串的创建就不是很耗时的部分,而且当你不使用大列表时,时间安排是愚蠢的。

    这个解决方案: 每个循环 11.6 毫秒 ± 153 微秒(平均值 ± 标准偏差。7 次运行,每次 100 次循环)

    Rakesh' 循环解决方案: 每个循环 25.8 毫秒 ± 819 微秒(平均值 ± 标准偏差。7 次运行,每次 10 次循环)

    RoadRunner 的 groupby: 每个循环 34.4 毫秒 ± 1.21 毫秒(平均值 ± 标准偏差。7 次运行,每次 10 次循环)

    【讨论】:

    • 当然,这仅在占位符实际上是一个字符串并且该搅拌没有出现在任何其他单词中时才有效。 IE。如果占位符是 None'Q''',它将不起作用
    • @tobias_k Fair nuff,为任何随机占位符修复它。
    猜你喜欢
    • 2023-01-09
    • 2020-07-20
    • 1970-01-01
    • 1970-01-01
    • 2018-01-10
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多