0
if type(prevtag_pair) == list:
    word_tag=[]
    for row in prevtag_pair:
        c =0
        x =[]
        for col in row:
            y=[]
            for pair in col: 
                if len(pair[1])> 0:
                    y.append(pair[0]+ '_'+pair[1])
                else: 
                    y.append(pair[0]+ '_' )
            x.append(y)
        word_tag.append(x)
    

我想把它转换成一个班轮。但我收到错误。任何帮助,将不胜感激。

if type(prevtag_pair) == list:
    word_tag = [[y.append(pair[0]+ '_'+pair[1]) if len(pair[1])> 0 else y.append(pair[0]+ '_' ) ] for row in prevtag_pair for col in row for pair in col]

如果可能,请更正它。谢谢你。

输入/输出数据:

[[[['挑战','D'],['机会','D'],['克服','P'],['挑战','D'],['机会','D' '], ['higher', 'D'], ['levelthan', 'A']], [['country', 'D'], ['face', 'P'],
['levels', 'N'], ['挑战', 'A'], ['民主', 'A'],
['基础', 'P'], ['进步', 'A']], [['挑战', 'A'], ['民主', 'P'], ['faces', 'N'], ['world', 'D'], ['level', 'A']], [[ '挑战', 'D'], ['机会', 'D'], ['进步', 'A'], ['陈述', 'D'], ['揭示', 'N'], ['idea', 'D'],
['挑战', 'D'], ['难度', ' D'], ['困难', 'A'],
['机会', 'D'], ['进步', 'A'], ['挑战', 'D'],
['克服', ' A'], ['挑战', 'D'], ['成功', 'P'],], ['克服', 'A'], ['挑战', 'D'], ['成功', 'P'],], ['克服', 'A'], ['挑战', 'D'], ['成功', 'P'],
['克服', 'A'], ['上升', 'A'], ['更高', 'D'],
['水平', 'A'], ['进步', 'A']] , [['challenges', ['o']],
['enlight', 'N'], ['deal', 'P'], ['ways', 'D'], ['like', ' A'], ['障碍', 'A'], ['enlights', 'P'], ['afford', 'P'], ['best', 'P'], ['result', ' D'],['挑战','P'],['喜欢','A'],['机会','D'],['进步','P'],['想要',' P'], ['deal', 'P'], ['smartly', 'P'], ['victorious', 'V'],
['宽松', 'C'], ['生活', 'P']], [['挑战', 'D'],
['问题', 'D'], ['机会', 'D' ], ['progress', 'A'],
['meansa', 'N'], ['challenge', 'P'], ['problem', 'D'],
['help', 'D'], ['development', 'D'], ['ex', 'P'], ['farmer', 'D'], ['challenging', 'A'], ['grow', 'P'], ['plants', 'A'],
['set', 'P'], ['target', 'P'], ['help', 'V'], ['发展', 'A']]]]

O/P: 错误出现 [none],[none],.....

4

2 回答 2

1

我希望这可以解决你的问题,

for row in prevtag_pair:
    word_tag = [[f'{pair[0]}_{pair[1]}' if len(pair[1]) > 0 else f'{pair[0]}_' for pair in col] for col in row ]
    print(word_tag)

输出:

[['challenge_D', 'opportunity_D', 'overcoming_P', 'challenge_D', 'opportunity_D', 'higher_D', 'levelthan_A'], ['country_D', 'face_P', 'levels_N', 'challenges_A', 'democracy_A', 'foundational_P', 'progress_A'], ['challenges_A', 'democracy_P', 'faces_N', 'world_D', 'level_A'], ['challenge_D', 'opportunity_D', 'progress_A', 'statement_D', 'reveals_N', 'idea_D', 'challenge_D', 'difficulty_D', 'hardship_A', 'opportunity_D', 'progress_A', 'challenge_D', 'overcome_A', 'challenge_D', 'succeed_P', 'overcoming_A', 'rise_A', 'higher_D', 'level_A', 'progess_A'], ["challenges_['o']", 'enlight_N', 'deal_P', 'ways_D', 'like_A', 'obstacles_A', 'enlights_P', 'afford_P', 'best_P', 'result_D', 'challenges_P', 'like_A', 'opportunity_D', 'progress_P', 'want_P', 'deal_P', 'smartly_P', 
'victorious_V', 'looser_C', 'life_P'], ['challenge_D', 'problem_D', 'opportunity_D', 'progress_A', 'meansa_N', 'challenge_P', 'problem_D', 'help_D', 'development_D', 'ex_P', 'farmer_D', 'challenging_A', 'grow_P', 'plants_A', 'set_P', 'target_P', 'help_V', 'development_A']]
于 2021-05-26T13:59:57.390 回答
1
word_tag = [[[f'{pair[0]}_{pair[1]}' if len(pair[1]) > 0 else f'{pair[0]}_' for pair in col] for col in row ]for row in prevtag_pair]

这很好用!

@Nanthakumar JJ:非常感谢...

于 2021-05-26T14:21:07.453 回答