【问题标题】:Sum of lists for each element of list1 with all in list2list1 中每个元素的列表总和,所有元素都在 list2 中
【发布时间】:2015-10-17 07:53:44
【问题描述】:

我想要制作从文件中读取行的脚本,而不是从每一行获取切片,将 1 行的所有切片与 2 行的所有切片组合,然后将上一步的所有切片与第 3 行组合。

例如,我们有

Stackoverflow (4)
python (3)
question (3)

我得到第一个包含(数字)字母切片的列表。

lst = ['Stac', 'tack', 'acko', 'ckov', 'kove', 'over', 'verf', 'erfl', 'rflo', 'flow']

然后我需要将它与第二个列表结合起来:

lst = ['pyt', 'yth', 'tho', 'hon']

期望的输出:

finallist = ['Stacpyt', 'tackpyt', 'ackopyt', 'ckovpyt', 'kovepyt', 'overpyt', 'verfpyt', 'erflpyt', 'rflopyt', 'flowpyt' 'Stacyth', 'tackyth', 'ackoyth', 'ckovyth', 'koveyth', 'overyth', 'verfyth', 'erflyth', 'rfloyth', 'flowyth', ..... ,  'erflhon', 'rflohon', 'flowhon']

然后是第三个列表:

lst = ['que', 'ues', 'est', 'sti', 'tio', 'ion']

finallist = ['Stacpytque', 'tackpytque', 'ackopytque', 'ckovpytque', 'kovepytque', 'overpytque', 'verfpytque', 'erflpytque', 'rflopytque', .... 'erflhonion', 'rflohonion', 'flowhonion']

我停留在需要综合结果入围的地方。

我正在尝试这样的代码片段,但它是错误的:

for i in lst:
    for y in finallist:
        finallist.append(i + y)

因此,如果 finallist 为空 - 它应该在第一次循环迭代中复制 lst,如果 finallist 不为空,它应该将每个元素与 lst 结合起来,依此类推。

【问题讨论】:

  • 4/3/3 的数字也在文件中?您的示例与您的文件的外观完全相同吗?
  • 是的,文件中也有数字。它代表列表的切片长度。如果是 Stackoverflow (12)。脚本应该使 ["Stackoverflo", "tackoverflow"]。老实说,我没有学习如何过滤字符串以获取 int,所以我暂时跳过它并制作了一个变量。
  • 文件可以包含任意数量的字符串,而不仅仅是3个。抱歉造成误解

标签: python


【解决方案1】:

我使用re.match() 来从您的文件中获取单词和整数值。

然后,我计算所有切片的子词并将它们添加到列表中,然后将其添加到全局列表中。

最后,感谢itertools.product(),我计算出您正在寻找的所有可能性,它的行为类似于嵌套的 for 循环。

然后,.join() 获得的元组,你就得到了你想要的最终列表。

from itertools import product
from re import match

the_lists = []

with open("filename.txt", "r") as file:
    for line in file:
        m = match(r'(.*) \((\d+)\)', line)
        word = m.group(1)
        num = int(m.group(2))
        the_list = [word[i:i+num] for i in range(len(word) - num + 1)]
        the_lists.append(the_list)

combinaisons = product(*the_lists)

final_list = ["".join(c) for c in combinaisons]

【讨论】:

  • 非常感谢。这正是我想要做的。
【解决方案2】:

使用ittertools

import itertools
list1 = ['Stac', 'tack', 'acko', 'ckov', 'kove', 'over', 'verf', 'erfl', 'rflo', 'flow']
list2 = ['pyt', 'yth', 'tho', 'hon']
list3 = ['que', 'ues', 'est', 'sti', 'tio', 'ion']

final_list = list(itertools.product(list(itertools.product(list1,list2)),list3))

这将为您提供所有组合,然后您可以将所有组合加入以获得您的字符串。

【讨论】:

  • 列表数等于字符串数。它可以超过 3 个。所以我做了一个循环,从每一行生成列表。在这个循环中,我想制作另一个将最终列表(计数为 0 时为空)与当前列表相结合的循环
  • 哦,抱歉,没看那部分。在这种情况下,使用包含所有项目的列表制作 for 循环,然后使用 itertools。
  • 我在循环中使用了 iter alllst = finallist(itertools.product(finallist,templist)),但我没有得到想要的输出。我得到[(('Stac', 'pyt'), 'que'), (('Stac', 'pyt'), 'ues'), (('Stac', 'pyt'), 'est'), (('Stac', 'pyt'), 'sti'), (('Stac', 'pyt'), 'tio'), (('Stac', 'pyt'), 'ion'), (('Stac', 'yth'), 'que'), (('Stac', 'yth'), 'ues') 项目不在一起。第二:如果 finallist 在开始时是空的。我最后得到空列表。解决它的唯一方法是让if count == 1: finallist = templist循环???
  • 您可能需要先使用“newlist = list2 + list2”,然后才能在 itertools 中使用它来获取净时间。它将两个列表(您在代码中获得)扁平化为一个列表。
【解决方案3】:
import itertools

def combine(lst):
    result = list(itertools.product(*lst))
    result = [''.join(item) for item in result]
    return result

list1 = ['Stac', 'tack', 'acko', 'ckov', 'kove', 'over', 'verf', 'erfl', 'rflo', 'flow']
list2 = ['pyt', 'yth', 'tho', 'hon']
list3 = ['que', 'ues', 'est', 'sti', 'tio', 'ion']

lst = [list1, list2, list3]  # append more list to lst, then pass lst to combination
print combine(lst)

将所有候选列表追加到lstcombine()函数将生成各种组合,然后将结果作为列表返回。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-02-08
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2016-11-19
    • 1970-01-01
    • 2018-09-17
    • 2018-07-07
    相关资源
    最近更新 更多