Python: Search inside dictionary for loose match










2















I have a python dictionary which looks as follows
key: leftname//rightname



The format is key: 'leftname // rightname)'



I want to ensure that each 'leftname' only appears once. If it appears again, I want to delete entire value for that key.



This is what this section of the dictionary should look like afterwards, with entries crossed out (deleted)



enter image description here



Notice how Ruby appears twice. This is fine, as I only need to ensure each 'Leftname' is unique. Each 'RightName' can be repeated.



I have tried to create a seperate array of names with str.split. I am not sure how to use regular expressions to filter out and delete the duplicates.
Quite frankly, I have no idea how I am going to accomplish that.










share|improve this question



















  • 1





    Part one of your task: bring your data into a more suitable structure than having two values in one string field.

    – Klaus D.
    Nov 14 '18 at 8:24











  • Part two: get that data as plain text. Images make it harder.

    – usr2564301
    Nov 14 '18 at 8:33












  • Yeah, probably worth adding what you've done so far as well. Hard to gauge what needs to be fixed if we don't know what you've made so far.

    – fixatd
    Nov 14 '18 at 8:38















2















I have a python dictionary which looks as follows
key: leftname//rightname



The format is key: 'leftname // rightname)'



I want to ensure that each 'leftname' only appears once. If it appears again, I want to delete entire value for that key.



This is what this section of the dictionary should look like afterwards, with entries crossed out (deleted)



enter image description here



Notice how Ruby appears twice. This is fine, as I only need to ensure each 'Leftname' is unique. Each 'RightName' can be repeated.



I have tried to create a seperate array of names with str.split. I am not sure how to use regular expressions to filter out and delete the duplicates.
Quite frankly, I have no idea how I am going to accomplish that.










share|improve this question



















  • 1





    Part one of your task: bring your data into a more suitable structure than having two values in one string field.

    – Klaus D.
    Nov 14 '18 at 8:24











  • Part two: get that data as plain text. Images make it harder.

    – usr2564301
    Nov 14 '18 at 8:33












  • Yeah, probably worth adding what you've done so far as well. Hard to gauge what needs to be fixed if we don't know what you've made so far.

    – fixatd
    Nov 14 '18 at 8:38













2












2








2








I have a python dictionary which looks as follows
key: leftname//rightname



The format is key: 'leftname // rightname)'



I want to ensure that each 'leftname' only appears once. If it appears again, I want to delete entire value for that key.



This is what this section of the dictionary should look like afterwards, with entries crossed out (deleted)



enter image description here



Notice how Ruby appears twice. This is fine, as I only need to ensure each 'Leftname' is unique. Each 'RightName' can be repeated.



I have tried to create a seperate array of names with str.split. I am not sure how to use regular expressions to filter out and delete the duplicates.
Quite frankly, I have no idea how I am going to accomplish that.










share|improve this question
















I have a python dictionary which looks as follows
key: leftname//rightname



The format is key: 'leftname // rightname)'



I want to ensure that each 'leftname' only appears once. If it appears again, I want to delete entire value for that key.



This is what this section of the dictionary should look like afterwards, with entries crossed out (deleted)



enter image description here



Notice how Ruby appears twice. This is fine, as I only need to ensure each 'Leftname' is unique. Each 'RightName' can be repeated.



I have tried to create a seperate array of names with str.split. I am not sure how to use regular expressions to filter out and delete the duplicates.
Quite frankly, I have no idea how I am going to accomplish that.







python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 9:49









iElden

684517




684517










asked Nov 14 '18 at 8:20









SamSam

385




385







  • 1





    Part one of your task: bring your data into a more suitable structure than having two values in one string field.

    – Klaus D.
    Nov 14 '18 at 8:24











  • Part two: get that data as plain text. Images make it harder.

    – usr2564301
    Nov 14 '18 at 8:33












  • Yeah, probably worth adding what you've done so far as well. Hard to gauge what needs to be fixed if we don't know what you've made so far.

    – fixatd
    Nov 14 '18 at 8:38












  • 1





    Part one of your task: bring your data into a more suitable structure than having two values in one string field.

    – Klaus D.
    Nov 14 '18 at 8:24











  • Part two: get that data as plain text. Images make it harder.

    – usr2564301
    Nov 14 '18 at 8:33












  • Yeah, probably worth adding what you've done so far as well. Hard to gauge what needs to be fixed if we don't know what you've made so far.

    – fixatd
    Nov 14 '18 at 8:38







1




1





Part one of your task: bring your data into a more suitable structure than having two values in one string field.

– Klaus D.
Nov 14 '18 at 8:24





Part one of your task: bring your data into a more suitable structure than having two values in one string field.

– Klaus D.
Nov 14 '18 at 8:24













Part two: get that data as plain text. Images make it harder.

– usr2564301
Nov 14 '18 at 8:33






Part two: get that data as plain text. Images make it harder.

– usr2564301
Nov 14 '18 at 8:33














Yeah, probably worth adding what you've done so far as well. Hard to gauge what needs to be fixed if we don't know what you've made so far.

– fixatd
Nov 14 '18 at 8:38





Yeah, probably worth adding what you've done so far as well. Hard to gauge what needs to be fixed if we don't know what you've made so far.

– fixatd
Nov 14 '18 at 8:38












2 Answers
2






active

oldest

votes


















2














You can do it like so:



output = 
seen = set()
for k,v in data.items():
leftname = v.split(' // ')[0]
if leftname not in seen:
seen.add(leftname)
output[k] = v


Where data is the dictionary holding your data.






share|improve this answer




















  • 1





    Downvoted because it was wrong before you added the seen = set() etc as in my answer.

    – AndyK
    Nov 14 '18 at 8:53












  • Perfect! Thanks :)

    – Sam
    Nov 14 '18 at 9:36


















0














Here's one working approach:



# simplified example
d = 1:'a // b', 2:'c // d', 3:'a // d', 4:'e // d'

d_new =
seen_leftname = set()
for key, val in d.items():
leftname = val.split(' // ')[0]
if leftname not in seen_leftname:
seen_leftname.add(leftname)
d_new[key] = val

print(d_new)
1: 'a // b', 2: 'c // d', 4: 'e // d'


You can also reset the keys:



d_new = 
seen_leftname = set()
key_new = 1
for val in d.values():
leftname = val.split(' // ')[0]
if leftname not in seen_leftname:
seen_leftname.add(leftname)
d_new[key_new] = val
key_new += 1

print(d_new)
1: 'a // b', 2: 'c // d', 3: 'e // d'





share|improve this answer
























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295719%2fpython-search-inside-dictionary-for-loose-match%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    You can do it like so:



    output = 
    seen = set()
    for k,v in data.items():
    leftname = v.split(' // ')[0]
    if leftname not in seen:
    seen.add(leftname)
    output[k] = v


    Where data is the dictionary holding your data.






    share|improve this answer




















    • 1





      Downvoted because it was wrong before you added the seen = set() etc as in my answer.

      – AndyK
      Nov 14 '18 at 8:53












    • Perfect! Thanks :)

      – Sam
      Nov 14 '18 at 9:36















    2














    You can do it like so:



    output = 
    seen = set()
    for k,v in data.items():
    leftname = v.split(' // ')[0]
    if leftname not in seen:
    seen.add(leftname)
    output[k] = v


    Where data is the dictionary holding your data.






    share|improve this answer




















    • 1





      Downvoted because it was wrong before you added the seen = set() etc as in my answer.

      – AndyK
      Nov 14 '18 at 8:53












    • Perfect! Thanks :)

      – Sam
      Nov 14 '18 at 9:36













    2












    2








    2







    You can do it like so:



    output = 
    seen = set()
    for k,v in data.items():
    leftname = v.split(' // ')[0]
    if leftname not in seen:
    seen.add(leftname)
    output[k] = v


    Where data is the dictionary holding your data.






    share|improve this answer















    You can do it like so:



    output = 
    seen = set()
    for k,v in data.items():
    leftname = v.split(' // ')[0]
    if leftname not in seen:
    seen.add(leftname)
    output[k] = v


    Where data is the dictionary holding your data.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 14 '18 at 8:48

























    answered Nov 14 '18 at 8:28









    David BarishevDavid Barishev

    319316




    319316







    • 1





      Downvoted because it was wrong before you added the seen = set() etc as in my answer.

      – AndyK
      Nov 14 '18 at 8:53












    • Perfect! Thanks :)

      – Sam
      Nov 14 '18 at 9:36












    • 1





      Downvoted because it was wrong before you added the seen = set() etc as in my answer.

      – AndyK
      Nov 14 '18 at 8:53












    • Perfect! Thanks :)

      – Sam
      Nov 14 '18 at 9:36







    1




    1





    Downvoted because it was wrong before you added the seen = set() etc as in my answer.

    – AndyK
    Nov 14 '18 at 8:53






    Downvoted because it was wrong before you added the seen = set() etc as in my answer.

    – AndyK
    Nov 14 '18 at 8:53














    Perfect! Thanks :)

    – Sam
    Nov 14 '18 at 9:36





    Perfect! Thanks :)

    – Sam
    Nov 14 '18 at 9:36













    0














    Here's one working approach:



    # simplified example
    d = 1:'a // b', 2:'c // d', 3:'a // d', 4:'e // d'

    d_new =
    seen_leftname = set()
    for key, val in d.items():
    leftname = val.split(' // ')[0]
    if leftname not in seen_leftname:
    seen_leftname.add(leftname)
    d_new[key] = val

    print(d_new)
    1: 'a // b', 2: 'c // d', 4: 'e // d'


    You can also reset the keys:



    d_new = 
    seen_leftname = set()
    key_new = 1
    for val in d.values():
    leftname = val.split(' // ')[0]
    if leftname not in seen_leftname:
    seen_leftname.add(leftname)
    d_new[key_new] = val
    key_new += 1

    print(d_new)
    1: 'a // b', 2: 'c // d', 3: 'e // d'





    share|improve this answer





























      0














      Here's one working approach:



      # simplified example
      d = 1:'a // b', 2:'c // d', 3:'a // d', 4:'e // d'

      d_new =
      seen_leftname = set()
      for key, val in d.items():
      leftname = val.split(' // ')[0]
      if leftname not in seen_leftname:
      seen_leftname.add(leftname)
      d_new[key] = val

      print(d_new)
      1: 'a // b', 2: 'c // d', 4: 'e // d'


      You can also reset the keys:



      d_new = 
      seen_leftname = set()
      key_new = 1
      for val in d.values():
      leftname = val.split(' // ')[0]
      if leftname not in seen_leftname:
      seen_leftname.add(leftname)
      d_new[key_new] = val
      key_new += 1

      print(d_new)
      1: 'a // b', 2: 'c // d', 3: 'e // d'





      share|improve this answer



























        0












        0








        0







        Here's one working approach:



        # simplified example
        d = 1:'a // b', 2:'c // d', 3:'a // d', 4:'e // d'

        d_new =
        seen_leftname = set()
        for key, val in d.items():
        leftname = val.split(' // ')[0]
        if leftname not in seen_leftname:
        seen_leftname.add(leftname)
        d_new[key] = val

        print(d_new)
        1: 'a // b', 2: 'c // d', 4: 'e // d'


        You can also reset the keys:



        d_new = 
        seen_leftname = set()
        key_new = 1
        for val in d.values():
        leftname = val.split(' // ')[0]
        if leftname not in seen_leftname:
        seen_leftname.add(leftname)
        d_new[key_new] = val
        key_new += 1

        print(d_new)
        1: 'a // b', 2: 'c // d', 3: 'e // d'





        share|improve this answer















        Here's one working approach:



        # simplified example
        d = 1:'a // b', 2:'c // d', 3:'a // d', 4:'e // d'

        d_new =
        seen_leftname = set()
        for key, val in d.items():
        leftname = val.split(' // ')[0]
        if leftname not in seen_leftname:
        seen_leftname.add(leftname)
        d_new[key] = val

        print(d_new)
        1: 'a // b', 2: 'c // d', 4: 'e // d'


        You can also reset the keys:



        d_new = 
        seen_leftname = set()
        key_new = 1
        for val in d.values():
        leftname = val.split(' // ')[0]
        if leftname not in seen_leftname:
        seen_leftname.add(leftname)
        d_new[key_new] = val
        key_new += 1

        print(d_new)
        1: 'a // b', 2: 'c // d', 3: 'e // d'






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 14 '18 at 8:51

























        answered Nov 14 '18 at 8:40









        AndyKAndyK

        918818




        918818



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53295719%2fpython-search-inside-dictionary-for-loose-match%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Barbados

            How to read a connectionString WITH PROVIDER in .NET Core?

            Node.js Script on GitHub Pages or Amazon S3