简体   繁体   English

追加到python中的空列表

[英]Append to empty list in python

I'm pretty new to Python and have a task where I am supposed to iterate through a sorted list to find the place where to insert an element into the list at the right place.我对 Python 非常陌生,并且有一项任务,我应该遍历排序列表以找到将元素插入列表中正确位置的位置。 So far I have managed to get it to work, except for when the list is empty.到目前为止,我已经设法让它工作,除非列表为空。 I have tried to read up on the "append"-function to see how I can do to not make it print "None", but without any results.我试图阅读“附加”功能,以了解如何不使其打印“无”,但没有任何结果。

If someone has a good answer to how I can adjust the first if-statement to make it possible to add the value x to the list and print the updated list it would be amazing.如果有人对我如何调整第一个 if 语句以使其能够将值 x 添加到列表并打印更新的列表有一个很好的答案,那将是惊人的。 I want the first test with the (2, []) to show [2].我希望 (2, []) 的第一个测试显示 [2]。

# Task 1a
# Create a function that places an element x in a sorted list

def insert_in_sorted (x, sorted_list):
    for i in range(len(sorted_list)):
        if not sorted_list:
            sorted_list.append(x)
            return sorted_list
        if x > max(sorted_list) or not sorted_list:
            sorted_list.append(x)
            return sorted_list
        elif sorted_list[i] > x:
            sorted_list.insert(i,x)
            return sorted_list
        elif sorted_list[i-1] and sorted_list[i+1] == x:
            sorted_list.insert(i,x)
            return sorted_list

print(insert_in_sorted(2,[]))
print(insert_in_sorted(5,[0,1,3,4]))
print(insert_in_sorted(2,[0,1,2,3,4]))
print(insert_in_sorted(2,[2,2]))

your look will only iterate if there are items in the list, so just take the first if outside the loop to check if the list is empty or not您的外观只会在列表中有项目时进行迭代,因此只需在循环外使用第一个 if 来检查列表是否为空

# Task 1a
# Create a function that places an element x in a sorted list

def insert_in_sorted(x, sorted_list):
    if not sorted_list:
        sorted_list.append(x)
        return sorted_list
    
    for i in range(len(sorted_list)):
        if x > max(sorted_list) or not sorted_list:
            sorted_list.append(x)
            return sorted_list
        elif sorted_list[i] > x:
            sorted_list.insert(i, x)
            return sorted_list
        elif sorted_list[i - 1] and sorted_list[i + 1] == x:
            sorted_list.insert(i, x)
            return sorted_list


print(insert_in_sorted(2, []))
print(insert_in_sorted(5, [0, 1, 3, 4]))
print(insert_in_sorted(2, [0, 1, 2, 3, 4]))
print(insert_in_sorted(2, [2, 2]))

OUTPUT输出

[2]
[0, 1, 3, 4, 5]
[0, 1, 2, 2, 3, 4]
[2, 2, 2]

it seems you are doing left insert看来你在做左插入

import bisect
def insert_in_sorted (x, sorted_list):

    bisect.insort_left(sorted_list,x)
    return sorted_list

print(insert_in_sorted(2,[]))
print(insert_in_sorted(5,[0,1,3,4]))
print(insert_in_sorted(2,[0,1,2,3,4]))
print(insert_in_sorted(2,[2,2]))

[2]
[0, 1, 3, 4, 5]
[0, 1, 2, 2, 3, 4]
[2, 2, 2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM