简体   繁体   English

根据条件更改列表列表的元素

[英]Change the elements of list of lists based on condition

I have the following nested list:我有以下嵌套列表:

original = [['B_S', 'O', 'O', 'O'],
            ['O', 'O', 'O', 'O', 'O', 'O', 'B_S', 'B_S', 'O', 'O'],
            ['O', 'B_S', 'O', 'O', 'B_S', 'B_S', 'B_S', 'O']]

There are only three kind of elements in the original list, ie, B_S , I_S ,and O .原始列表中只有三种元素,即B_SI_SO I want to change the elements based on aa specific condition: If an elements starts with B-prefix (ie, B_S) the following element should be changed to start with I-prefix if it had B-prefix .我想根据特定条件更改元素:如果元素以B- prefix (即 B_S)开头,则以下元素应更改为以I-prefix开头(如果它具有B- prefix )。 The desired output in this case is:在这种情况下,所需的 output 是:

desired = [['B_S', 'O', 'O', 'O'],
            ['O', 'O', 'O', 'O', 'O', 'O', 'B_S', 'I_S', 'O', 'O'],
            ['O', 'B_S', 'O', 'O', 'B_S', 'I_S', 'B_S', 'O']]

It worked with this solution:它适用于这个解决方案:

for ls in original:
    for i in range(0,len(ls)):
        if ls[i] == 'B_S' and ls[i+1] == 'B_S':
            ls[i+1] = 'I_S'

But it takes a long time with a large dataset... is there any way to improve the code performance?但是大数据集需要很长时间......有什么办法可以提高代码性能?

You might want to look into multiprocessing:您可能想研究多处理:

from multiprocessing import Pool
import os


original = [['B_S', 'O', 'O', 'O'],
            ['O', 'O', 'O', 'O', 'O', 'O', 'B_S', 'B_S', 'O', 'O'],
            ['O', 'B_S', 'O', 'O', 'B_S', 'B_S', 'B_S', 'O']]


def change(sub_list):
    len_ = len(sub_list)
    cnt = 0
    while cnt < len_:
        if sub_list[cnt] == 'B_S' and sub_list[cnt+1] == 'B_S':
            sub_list[cnt+1] = 'I_S'
        cnt += 1
    return sub_list


if __name__ == '__main__':
    results = []
    for result in Pool(processes=os.cpu_count()).map(change, original[:]):
        results.append(result)
    print(results)

This will just spilt your original list into sublists, and treat them each individually before combining them back together.这只会将您的原始列表溢出到子列表中,并在将它们组合在一起之前单独处理它们。

This can for sure be further improved, as other comments already suggested.正如其他评论已经建议的那样,这肯定可以进一步改进。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM