Python两个列表位置对齐方法研究 您所在的位置:网站首页 python输出表格对齐 Python两个列表位置对齐方法研究

Python两个列表位置对齐方法研究

2023-09-08 04:27| 来源: 网络整理| 查看: 265

先插入Python切片研究:

参考链接:

https://www.cnblogs.com/mtn007/p/12074952.html

在NLP任务中,经常会处理序列问题,比如把一段英文字符输入某个工具,输出的列表中数字会有变化,但有时还需要和原始列表进行位置对齐,比如对齐词性信息,标签信息等。本文针对这个问题,提出了自己的一个小算法。伪代码如下:

列表对齐算法

位置对齐

List出栈,pop函数,单词可能重复,但位置不会重复(0,1,...,n)标准的出一个数据,对齐的出一个数据,然后判断两个数据是否相同,如果相同则加入返回列表,如果不相同则对齐的列表继续出一个数据,再重复比较

     3.利用了列表位置一定,虽然具体数据可能会重复,但列表位置一定,不会重复。故可以利用来进行对齐操作。此外每次出列表最前端的数据,保证可以对齐。且出队列表数据越来越少,适合比较

示例代码:

def query_loc(orgSent, cmpSent): sentList = [] while len(cmpSent) > 0 and len(orgSent) > 0: popWord = orgSent.pop(0) cmpWord = cmpSent.pop(0) num = 1 while popWord != cmpWord: cmpWord += cmpSent.pop(0) num += 1 sentList.append((popWord, num)) return sentList #与新位置一一对应 def transform2postion(sentList): orig_to_tok_List = [] pointer = 1 #有,故1 for i, (word, num) in enumerate(sentList): orig_to_tok_List.append((word, pointer))#词,位置 pointer += num #下一个词开始位置 return orig_to_tok_List

#一步对应

def transform2postion(sentList, orgSent): orig_to_tok_List = [] pointer = 1 for i, (word, num) in enumerate(sentList): orig_to_tok_List.append((word, pointer)) pointer += num correspondingList = [] for i, word in enumerate(orgSent)://原始字列表 start = i word2, start_s = orig_to_tok_List[start] //词对应 word3, num = sentList[start] //原始对应 assert word == word2 assert word == word3 end_s = start_s + num - 1 //只有一个词+0,有num个词,+num-1 correspondingList.append((word, start_s, end_s)) return correspondingList


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有