如何使用一个衬里而不是 'for loop ' 也包含 if-else 语句(给出的例子)
How to use one liner instead of 'for loop ' which also contains if-else statement (example given)
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]
如果可能请更正。谢谢。
I/P 数据:
[[[['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']]]]
O/P:错误出现为 [none]、[none]、.....
希望这可以解决您的问题,
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']]
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 J J:非常感谢...
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]
如果可能请更正。谢谢。
I/P 数据:
[[[['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']]]]
O/P:错误出现为 [none]、[none]、.....
希望这可以解决您的问题,
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']]
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 J J:非常感谢...