从保存在2D列表中的三元组单词构造文本

安德烈亚斯(Andreas Kreouzos)

我目前有一个文本,其单词另存为2D列表中的三胞胎。

我的2D清单:

[['Python', 'is', 'an'], ['interpreted,', 'high-level', 'and'], ['general-purpose', 'programming', 'language.'], ["Python's", 'design', 'philosophy'], ['emphasizes', 'code', 'readability'], ['with', 'its', 'notable'], ['use', 'of', 'significant'], ['whitespace.', 'Its', 'language'], ['constructs', 'and', 'object-oriented'], ['approach', 'aim', 'to'], ['help', 'programmers', 'write'], ['clear,', 'logical', 'code'], ['for', 'small', 'and'], ['large-scale', 'projects.']]

我正在创建一个Python代码,该代码选择一组随机的三元组,然后尝试使用最后两个单词并选择以这两个单词开头的三元组来创建新的随机文本。最终,当我写了200个单词或无法选择其他三元组时,程序结束。

到目前为止的代码:

import random

with open(r'c:\\python\4_TRIPLETS\Sample.txt', 'r') as file:
    data = file.read().replace('\n', '').split()
    lines = [data[i:i + 3] for i in range(0, len(data), 3)]
    random.shuffle([random.shuffle(i) for i in lines])

first_triplet = random.choice(lines)
last_two = first_triplet[1:3]


output_text=[]
while True:
    candidates = [t for t in lines if t[0:2] == last_two]
    if not candidates:
        break
    
    next_triplet = random.choice(candidates)
    last_two = next_triplet[1:3]
    output_text.append(next_triplet)

我无法自动执行重复的搜索匹配并将它们存储在新列表中的过程。

有任何想法吗?

碎屑

您可以使用递归函数(更改了代码的某些部分,请检查注释):

import random

#adding ["is", "an", "experiment"] to check if it works (no other triplet were present that satisfy the condition)
lines = [['Python', 'is', 'an'], ['interpreted,', 'high-level', 'and'], ['general-purpose', 'programming', 'language.'], ["Python's", 'design', 'philosophy'], ['emphasizes', 'code', 'readability'], ['with', 'its', 'notable'], ['use', 'of', 'significant'], ['whitespace.', 'Its', 'language'], ['constructs', 'and', 'object-oriented'], ['approach', 'aim', 'to'], ['help', 'programmers', 'write'], ['clear,', 'logical', 'code'], ['for', 'small', 'and'], ['large-scale', 'projects.'], ["is", "an", "experiment"]]

first_triplet = ['Python', 'is', 'an'] # random.choice(lines)


def appendNextTriplet(output_text, lines):
    if len(output_text) >= 200:
        return output_text
    candidates = [t for t in lines if t[:2] == output_text[-2:]]
    if not candidates:
        return output_text
    next_triplet = random.choice(candidates)
    output_text += next_triplet # changed from append to concatenation, it was not correct
    return appendNextTriplet(output_text, lines)

print(appendNextTriplet(first_triplet, lines)) # ['Python', 'is', 'an', 'is', 'an', 'experiment']

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从保存在2D列表中的三胞胎单词构造句子

来自分类Dev

从对列表创建三元组列表,以便所有三元组子集都存在于对列表中

来自分类Dev

从列表中删除相邻号码的三元组

来自分类Dev

使用(x,y,value)的三元组数据创建Numpy 2D数组

来自分类Dev

将三元组的三元组列表转换成字典

来自分类Dev

如何从SCALA中的三元组列表中创建多图?

来自分类Dev

在文本文件中查找三元组

来自分类Dev

使用无限性列表在Haskell中的勾股三元组

来自分类Dev

验证JSON-LD中的三元组

来自分类Dev

验证JSON-LD中的三元组

来自分类Dev

目标架构三元组中“ pc”的含义

来自分类Dev

如何删除三元组列表中每个元组的第二个元素?

来自分类Dev

RDF重复三元组

来自分类Dev

三元组的最佳合并

来自分类Dev

编号三元组

来自分类Dev

三元组的稀疏矩阵

来自分类Dev

编号三元组

来自分类Dev

处理三元组的重复

来自分类Dev

如何在SPARQL中构造结合两个过渡性封闭的新三元组?

来自分类Dev

将列表数组转换为元组/三元组数组

来自分类Dev

如何区分SQL三元组与显式三元组?

来自分类Dev

三元组的组的列表到地图一对键

来自分类Dev

如何编写一个接受一个数字并返回一个三元组列表的函数“三元组”?

来自分类Dev

如何在满足给定条件的榆树中创建一组三元组?

来自分类Dev

如何在满足给定条件的榆树中创建一组三元组?

来自分类Dev

将列表转换为字符串三元组序列

来自分类Dev

分离/访问像[[[[x,y],z],p]这样的三元组列表?

来自分类Dev

将python字典转换为三元组列表时出现问题?

来自分类Dev

其范围在(1,2)之间的三元组

Related 相关文章

热门标签

归档