Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
60 views
in Technique[技术] by (71.8m points)

python - Is my logic wrong in writing this function? get_elements_that_equal_10_at_a_value

Write a function called "get_elements_that_equal_10_at_a_value".

Given a dictionary and a key, "get_elements_that_equal_10_at_a_value" returns a list containing all the elements of the list located at the given key that are equal to ten.

Notes:

  • If the list is empty, it should return an empty list.
  • If the list contains no elements that are equal to 10, it should return an empty list.
  • If the value at the given key is not a list, it should return an empty list.
  • If the key doesn't exist or its value is None, it should return an empty list.
obj1 = {'key': [1000, 10, 50, 10]}
output1 = get_elements_that_equal_10_at_a_value(obj1, 'key')
print(output1) # --> [10, 10]
obj2 = {'key': 10}
output2 = get_elements_that_equal_10_at_a_value(obj2, 'key')
print(output2) # --> []

My logic using the if-else approach:

def get_elements_that_equal_10_at_a_value(obj, key):
    new_d = obj.copy()
    lst = []

    if new_d[key] == [] or type(new_d[key]) == int:
        return []
    elif 10 in obj[key]:
        for x in obj[key]:
            if 10 == x:
                lst.append(x)
        return lst
    elif new_d.keys == None or new_d[key] == None:
        return []
    else:
        return []

Error

======================================================================
ERROR: test_4 (test_methods.TestScript)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/usr/src/app/test_methods.py", line 43, in test_4
    self.assertEqual(main.get_elements_that_equal_10_at_a_value(obj, 'key'),
  File "/usr/src/app/main.py", line 5, in get_elements_that_equal_10_at_a_value
    if new_d[key] == [] or type(new_d[key]) == int:
KeyError: 'key'

----------------------------------------------------------------------
Ran 5 tests in 0.001s

FAILED (errors=1)
question from:https://stackoverflow.com/questions/65640899/is-my-logic-wrong-in-writing-this-function-get-elements-that-equal-10-at-a-valu

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can greatly simply your code by considering just three cases.

You first simply check if the key is in the obj, returning an empty list if not.

Then, you check the type, and if its anything but a list, you return an empty list.

Finally, use a list comprehension to extract the 10s in the list and return them.

def get_elements_that_equal_10_at_a_value(obj, key):
    if key not in obj:
        return []
    value = obj[key]
    if type(value) != list:
        return []
    return [val for val in value if val == 10]

obj1 = {'key': [1000, 10, 50, 10]}
output1 = get_elements_that_equal_10_at_a_value(obj1, 'key')
print(output1) # --> [10, 10]

obj2 = {'key': 10}
output2 = get_elements_that_equal_10_at_a_value(obj2, 'key')
print(output2) # --> []

Edit: You could simplify this even further by making use of dict.get(), setting obj[key] to be the empty list if the key does not exist. Thanks to @m-z for suggesting this.

def get_elements_that_equal_10_at_a_value(obj, key):
    value = obj.get(key, [])
    if type(value) != list:
        return []
    return [val for val in value if val == 10]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...