如何检查列表a是否大于列表b然后替换它

所以我想创建一个比较列表,其中我有一个列表 A(Old_list),其中包含:


{'name': 'Jesus and Mary', 'sizeslist': ['Low', 'Medium', 'High']}

和一个列表 B(产品),其中包含


{'name': 'Apple and Juice', 'sizeslist': None}

所以我一开始做的就是检查sizeslist的长度是否大于list B,那么就应该替换掉。


old_list = [{'name': 'Jesus and Mary', 'sizeslist': ['Low', 'Medium', 'High']}]


while True:

    product = [{'name': 'Apple and Juice', 'sizeslist': None}]


    if product not in old_list:


        a = product['sizeslist']


        if old_list != []:

            old_list_value = old_list[0]['sizeslist']


            if len(old_list_value) < len(a):

                print("Higher than old_list!")

                old_list[0] = product

                break


            elif len(old_list_value) > len(a):

                old_list[0] = product

                break

        else:

            old_list.append(product)

问题是我得到了object of type 'NoneType' has no len(),我的问题是如何改进代码,这样我就不会出现没有 len() 的错误,并且还能够只更改大小列表而不是整个列表。


编辑:


old_list = {'name': 'Jesus and Mary', 'sizes': ['Low', 'Medium', 'High']}


while True:

    new_list = {'name': 'Apple and Juice', 'sizes': None}


    try:

        if new_list['sizes'] not in old_list['sizes']:


                if old_list['sizes'] < new_list['sizes']:

                    print("New element!!!")

                    old_list['sizes'] = new_list['sizes']

                    break


                elif old_list['sizes'] > new_list['sizes']:

                    old_list['sizes'] = new_list['sizes']

                    break


        else:

            randomtime = random.randint(5, 10)

            time.sleep(randomtime)

            continue


    except Exception as err:

        logger.error(err)

        randomtime = random.randint(1, 2)

        time.sleep(randomtime)

        continue


子衿沉夜
浏览 126回答 2
2回答

慕森王

我假设您要比较'sizeslist'而不是整个字典。如果是这样,您应该考虑您'sizeslist'可能不是listbut 的情况None。这是处理这个问题的方法。a = {'name': 'Jesus and Mary', 'sizeslist': ['Low', 'Medium', 'High']}b = {'name': 'Apple and Juice', 'sizeslist': None}listA = a['sizeslist']listB = b['sizeslist']if not listB or (listA != None and len(listA) > len(listB)):&nbsp; &nbsp; b['sizeslist'] = a['sizeslist']else:&nbsp; &nbsp; print("Nope")print(b) # -> {'name': 'Apple and Juice', 'sizeslist': ['Low', 'Medium', 'High']}

开满天机

if old_list != []:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; old_list_value1 = old_list['sizeslist']&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; old_list_value = list(oldlist_value1[0])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if len(old_list_value) < len(a):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; print("Higher than old_list!")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; old_list[0] = product&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; elif len(old_list_value) > len(a):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; old_list[0] = product&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break这可能会奏效。另请注意,您正在比较字符串长度,其中 old_list[0] 的长度为 3 个字符, None 什么都不是。尝试将 None 更改为某个值。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python