pycharm AssertinputList == inputListSorted

8aqjt8rx  于 2023-08-05  发布在  PyCharm
关注(0)|答案(2)|浏览(103)

你能帮我,我的代码有什么不好?

def generalized_price_sorter_expensive_cheap_assert(inputList, typeOfSort):
    #print(inputList)
    if typeOfSort == "cheap":
        inputListSorted = inputList.copy()
        inputListSorted.sort()

        if inputList == inputListSorted:  ##compare first list to second list, if is equal = good
            print("Cheap sorter is OK")

        else:
            print("Cheap sorter is NOT OK")

    if typeOfSort == "expensive":
        inputListSorted = inputList.copy()
        inputListSorted.sort(reverse=True)
        if inputList == inputListSorted:
            print("Expensive sorter is OK")
        else:
            print("Expensive sorter is NOT OK")

    print("LIST FROM WEB:")
    print(inputList)
    print("CORRECTLY SORTED LIST")
    print(inputListSorted)

    assert inputList == inputListSorted

字符串
我的Assert错误:

<selenium.webdriver.remote.webelement.WebElement (session="7d749d08e3af4a8efc0322859c6b3e2e", element="0D305CB722AA126A875927A62168E5F6_element_91")>
[14818, 13398, 12958, 12138, 12058, 11038, 12178, 11938, 12018, 11238, 11178, 11498, 10478, 11378, 11558, 10838, 11318, 11858, 11458, 10938]
Expensive sorter is NOT OK
LIST FROM WEB:
[14818, 13398, 12958, 12138, 12058, 11038, 12178, 11938, 12018, 11238, 11178, 11498, 10478, 11378, 11558, 10838, 11318, 11858, 11458, 10938]
CORRECTLY SORTED LIST
[14818, 13398, 12958, 12178, 12138, 12058, 12018, 11938, 11858, 11558, 11498, 11458, 11378, 11318, 11238, 11178, 11038, 10938, 10838, 10478]

iszxjhcz

iszxjhcz1#

您需要将排序列表分配给变量,因为排序不是在原地完成的。

inputListSorted = inputList.copy()
inputListSorted = sorted(inputListSorted)

# Or in a single line

inputListSorted = sorted(inputList.copy())

字符串

wlwcrazw

wlwcrazw2#

你会得到Assert错误,因为你的列表中元素的顺序是不同的。如果你想要check if both lists have the same elements,你可以这样做:

import collections

def assert_same_elements(left, right):
    assert collections.Counter(left) == collections.Counter(right)

list_from_web = [
    14818, 13398, 12958, 12138, 12058, 11038, 12178, 11938,
    12018, 11238, 11178, 11498, 10478, 11378, 11558, 10838,
    11318, 11858, 11458, 10938
]

correctly_sorted_list = [
    14818, 13398, 12958, 12178, 12138, 12058, 12018, 11938,
    11858, 11558, 11498, 11458, 11378, 11318, 11238, 11178,
    11038, 10938, 10838, 10478
]

assert_same_elements(list_from_web, correctly_sorted_list)

字符串

相关问题