-2

I want to remove all <start> and <end> from a dictionary as:

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>',]}
NameVergessen
  • 598
  • 8
  • 26
A_B_Y
  • 332
  • 2
  • 8
  • Iterate through the dictionary, iterate through the list, remove / replace what needs to be changed. What have you tried, where are you stuck? – luk2302 Dec 18 '22 at 14:11

2 Answers2

1

You can use str.replace to replace <start>/<end> with an empty string:

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)

Prints:

{
    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",
    ],
}
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
0

try:

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

You may want to add a strip() after the second replace to get rid of the whitespace.

NameVergessen
  • 598
  • 8
  • 26