So I wrote this code with the help of Stack Overflow users..
def get_name(string):
return string.replace(" ", "").replace("-", "")
def keepTabs(input_list: list):
output = {}
for entry in input_list:
name = get_name(entry)
if name not in output:
output[name] = 0
if " " in entry:
output[name] = 1
if "--" in entry:
output[name] -= 1
if "->" in entry:
output[name] == [name 1]
return output
print(keepTabs(["Jim--", "John--", "Jordan--", "Jim ", "John--", "Jeff--", "June ", "June->Jim]))
In this code, every individual "Jim", "John" etc all start with 0 and they earn or lose points depending on where they get "--", which subtracts one away from them or " ", which adds one to their existent points... Now I want to edit this so that if there's a "->" provided in the list, for instance if in the list there's "John->Jim", all the points that John has, will be added to Jim's points...
I have tried to implement it as shown in the code above, but it didn't work so, what should I change to make sure this works??
Please use the simplest version of code possible since I'm very new to this :)
CodePudding user response:
My revision-
def get_name(string):
return string.replace(" ", "").replace("-", "")
def keepTabs(input_list: list):
output = {}
for entry in input_list:
if '->' in entry:
names = entry.split('->')
output[names[1]] = output[names[0]]
output[names[0]] = 0
else:
name = get_name(entry)
if name not in output:
output[name] = 0
if " " in entry:
output[name] = 1
if "--" in entry:
output[name] -= 1
return output
print(keepTabs(["Jim--", "John--", "Jordan--", "Jim ", "John--", "Jeff--", "June ", "June->Jim]))
CodePudding user response:
I don't think you need to use any kind of string replacement for this. How about:
def keepTabs(lst):
output = dict()
for item in lst:
t = item.split('->')
if len(t) == 2:
if t[1] in output:
output[t[1]] = output.get(t[0], 0)
else:
if item.endswith('--'):
x = -1
elif item.endswith(' '):
x = 1
else:
continue
if (name := item[:-2]) in output:
output[name] = x
else:
output[name] = x
return output
print(keepTabs(["Jim--", "John--", "Jordan--", "Jim ", "John--", "Jeff--", "June ", "June->Jim"]))