如何从字典中的列表中删除一个单词



我想从字典中删除所有的<start><end>:

my_dict = {1:['<start> the woman is slicing onions <end>',
'<start> a woman slices a piece of onion with a knife <end>',
'<start> a woman is chopping an onion <end>',
'<start> a woman is slicing an onion <end>',
'<start> a woman is chopping onions <end>',
'<start> a woman is slicing onions <end>',
'<start> a girl is cutting an onion <end>'],
2: ['<start> a large cat is watching and sniffing a spider <end>',
'<start> a cat sniffs a bug <end>',
'<start> a cat is sniffing a bug <end>',
'<start> a cat is intently watching an insect crawl across the floor <end>',
'<start> the cat checked out the bug on the ground <end>',
'<start> the cat is watching a bug <end>',
'<start> a cat is looking at an insect <end>'],
3:['<start> a man is playing a ukulele <end>',
'<start> a man is playing a guitar <end>',
'<start> a person is playing a guitar <end>',]}

您可以使用str.replace<start>/<end>替换为空字符串:

my_dict = {
1: [
"<start> the woman is slicing onions <end>",
"<start> a woman slices a piece of onion with a knife <end>",
"<start> a woman is chopping an onion <end>",
"<start> a woman is slicing an onion <end>",
"<start> a woman is chopping onions <end>",
"<start> a woman is slicing onions <end>",
"<start> a girl is cutting an onion <end>",
],
2: [
"<start> a large cat is watching and sniffing a spider <end>",
"<start> a cat sniffs a bug <end>",
"<start> a cat is sniffing a bug <end>",
"<start> a cat is intently watching an insect crawl across the floor <end>",
"<start> the cat checked out the bug on the ground <end>",
"<start> the cat is watching a bug <end>",
"<start> a cat is looking at an insect <end>",
],
3: [
"<start> a man is playing a ukulele <end>",
"<start> a man is playing a guitar <end>",
"<start> a person is playing a guitar <end>",
],
}
for v in my_dict.values():
v[:] = (s.replace("<start>", "").replace("<end>", "").strip() for s in v)
print(my_dict)

打印:

{
1: [
"the woman is slicing onions",
"a woman slices a piece of onion with a knife",
"a woman is chopping an onion",
"a woman is slicing an onion",
"a woman is chopping onions",
"a woman is slicing onions",
"a girl is cutting an onion",
],
2: [
"a large cat is watching and sniffing a spider",
"a cat sniffs a bug",
"a cat is sniffing a bug",
"a cat is intently watching an insect crawl across the floor",
"the cat checked out the bug on the ground",
"the cat is watching a bug",
"a cat is looking at an insect",
],
3: [
"a man is playing a ukulele",
"a man is playing a guitar",
"a person is playing a guitar",
],
}

try:

my_dict = {k : [s.replace("<start>", "").replace("<end>", "") for s in l] for k, l in my_dict.items() }

您可能需要在第二次替换后添加strip()以消除空白。

相关内容

  • 没有找到相关文章