Home > Mobile >  How can I exclude something if a specific string is provided in the output?
How can I exclude something if a specific string is provided in the output?

Time:11-23

So I wrote this code with the help of Stack Overflow users that transfers points to an individual based on whether " " or "--" appears behind an individual.

def get_name(input):
    return input.replace(" ", "").replace("-", "")
def keep(actions: list):
    g = {}
    for s in actions:
        if '->' in s:
            names = s.split('->')
            g[names[1]] = g[names[0]]
            g[names[0]] = 0
        else:
            name = get_name(s)
            if name not in g:
                g[name] = 0
            if "  " in s:
                g[name]  = 1
            if "--" in s:
                g[name] -= 1
    return {x:g[x] for x in g if g[x] != 0}
print(keep(["Jim  ", "John--", "Jeff  ", "Jim  ", "John--", "John->Jeff",
"Jeff--", "June  ", "Home->House"]))

So most of the program is alright, however, when I put "Home->House" into it, it returns a KeyError. I kinda understand why it does that, but I'm clueless as to how to fix that...

I tried browsing the internet for solutions but all they recommended was to use .get(), which doesn't really help me solve the issue.

How can I make my output just neglect if like an element doesn't have " " or "--" in it... like how can I make sure if an input is just "Jim" instead of "Jim " or "Jim--", the function would just neglect it...

So in my example, if the input for keep is

["Jim  ", "John--", "Jeff  ", "Jim  ", "John--", "John->Jeff", "Jeff--", "June  ", "Home->House "] 

the output would be

{'Jeff': -2, 'June': 1, 'Jim': 2}

instead of KeyError

CodePudding user response:

You get KeyError because g[names[1]] = g[names[0]] is trying to access element in dictionary that isn't there. You get the same issue with simple print(keep(["John->Jeff"])), because there are no or -- actions executed first to initialise those keys ("John" and "Jeff" in g

Based on your desired output you want to ignore such actions that are for non existing keys. Add if names[1] in g and names[0] in g: into your keep implementation i.e. Edit: also g[names[1]] = g[names[0]] needs to change to g[names[1]] = g[names[0]] to product correct outcome.

def get_name(input):
    return input.replace(" ", "").replace("-", "")

def keep(actions: list):
    g = {}
    for s in actions:
        if "->" in s:
            names = s.split("->")
            if names[1] in g and names[0] in g:
                g[names[1]]  = g[names[0]]
                g[names[0]] = 0
        else:
            name = get_name(s)
            if name not in g:
                g[name] = 0
            if "  " in s:
                g[name]  = 1
            if "--" in s:
                g[name] -= 1
    return {x: g[x] for x in g if g[x] != 0}

CodePudding user response:

In your code, when you get Home->House, it's the first appearence of both the keys. That's why you get KeyError when you try to execute g[names[1] = g[names[0]]: g[names[0]] is g['Home'], but this entry doesn't exist in your dict. You can simply solve swapping the order of the lines:

if '->' in s:
   names = s.split('->')
   g[names[0]] = 0
   g[names[1]] = g[names[0]]

To neglect strings which haven't the " " or the "--", you can simply add an if before performing get_name(s):

else:
   if '  ' in s or '--' in s:
      name = get_name(s)
      if name not in g:
         g[name] = 0
      if "  " in s:
         g[name]  = 1
      if "--" in s:
         g[name] -= 1

This code returns the expected output

CodePudding user response:

Your code actually does ignore examples which do not include " " or "--" in the else-section of your function.

The KeyError occurs because neither "Home" nor "House" appear in the List of input strings before you try to move the entry of "Home" into the entry of "House". Because the keys "Home" and "House" are not in your dictionary g you get a KeyError.

Below is a solution which does what you want.

def get_name(input):
    return input.replace(" ", "").replace("-", "")

def keep(actions: list):
    g = {}
    for s in actions:
        if '->' in s:
            names = s.split('->')
            if names[0] in g.keys() and names[1] in g.keys():
                g[names[1]] = g[names[0]]   g[names[1]]
                g[names[0]] = 0
        else:
            name = get_name(s)
            if name not in g:
                g[name] = 0
            if "  " in s:
                g[name]  = 1
            if "--" in s:
                g[name] -= 1
    return {x:g[x] for x in g if g[x] != 0}
print(keep(["Jim  ", "John--", "Jeff  ", "Jim  ", "John", "John--", "John->Jeff",
"Jeff--", "June  ", "Home->House"]))

I added a check to make sure the keys exist in dict d when the input string includes a "->".

In order to get the output you indicated in your question, which includes 'Jeff': -2, you also need to make sure to add the value of the original key to the value of the new key. This is done in line

g[names[1]] = g[names[0]]   g[names[1]]

Otherwise the output will say 'Jeff': -3 as the input string "Jeff " will be ignored.

  • Related