Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在不使用insert的情况下向特定位置的列表中添加内容_Python - Fatal编程技术网

Python 如何在不使用insert的情况下向特定位置的列表中添加内容

Python 如何在不使用insert的情况下向特定位置的列表中添加内容,python,Python,请给我一些建议。我对编程相当陌生,正在做一项作业,需要在指定位置的列表中添加值。赋值的参数是必须使用循环,并且不能使用除range()或append()之外的任何内置函数 我得到的错误是,从底部开始的第四行说“TypeError:只能将list(而不是“int”)连接到list”,我不知道如何修复这个问题。 任何建议都将不胜感激!请注意-因为这是一个作业,我不是在寻找代码,而是在哪里出了问题,以及如何修复它,因为我想学习和理解 my_list = [1, 2, 3, 4, 5, 4, 1, 4,

请给我一些建议。我对编程相当陌生,正在做一项作业,需要在指定位置的列表中添加值。赋值的参数是必须使用循环,并且不能使用除range()或append()之外的任何内置函数

我得到的错误是,从底部开始的第四行说“TypeError:只能将list(而不是“int”)连接到list”,我不知道如何修复这个问题。 任何建议都将不胜感激!请注意-因为这是一个作业,我不是在寻找代码,而是在哪里出了问题,以及如何修复它,因为我想学习和理解

my_list = [1, 2, 3, 4, 5, 4, 1, 4, 6]

#function 1 - takes list as parameter and returns length
def length(my_list):
    count = 0
    for x in my_list:
        count = count + 1
    return count

#function 5 - returns a copy of the list with the value inserted at the specified pos
def insert_value(my_list, value, insert_position):
    count = 0
    new_list = []
    if insert_position > length(my_list):
        new_list = my_list.append(value)
    elif insert_position < 0:
        new_list = value + my_list
    while count < insert_position:
        new_list = my_list[:insert_position]
        count= count + 1
        new_list = new_list + value
        new_list = new_list[insert_position+1:]
    return new_list
print(insert_value(my_list, 11, 6))
my_list=[1,2,3,4,5,4,1,4,6]
#函数1-将列表作为参数并返回长度
def长度(我的_列表):
计数=0
对于my_列表中的x:
计数=计数+1
返回计数
#函数5-返回在指定位置插入值的列表副本
def insert_值(我的_列表、值、插入位置):
计数=0
新列表=[]
如果插入位置>长度(我的列表):
新建列表=我的列表。追加(值)
elif插入位置<0:
新建列表=值+我的列表
计数<插入位置时:
新建列表=我的列表[:插入位置]
计数=计数+1
新建列表=新建列表+值
新建列表=新建列表[插入位置+1:]
返回新列表
打印(插入_值(我的_列表,11,6))

您遇到的问题是无法将整数添加到列表中。只能将两个类似列表的对象添加到一起

因此,
new_list=[value]+my_list
用于该特定情况

一般来说,我会使用列表切片。例如:

original_list = [0,1,2,3,5,6,7]
value_to_insert = 4
position_to_insert = 4
new_list = original_list[:position_to_insert] + [value_to_insert] + original_list[position_to_insert:]
如果必须使用循环:

new_list = []
for i in range(length(my_list)):
    new_list.append(my_list[i])
    if i == insert_position:
        new_list.append(my_list(value_to_insert))
return new_list
最后:

my_list = [1, 2, 3, 4, 5, 4, 1, 4, 6]

#function 1 - takes list as parameter and returns length
def length(my_list):
    count = 0
    for x in my_list:
        count = count + 1
    return count

#function 5 - returns a copy of the list with the value inserted at the specified pos
def insert_value(my_list, value, insert_position):
    new_list = []
    for i in range(length(my_list)):
        new_list.append(my_list[i])
        if i == insert_position:
            new_list.append(value)
    return new_list
print(insert_value(my_list, 11, 6))

正如您在问题中所说,您可以使用
append()
,因此您应该使用
new\u list=new\u list+value
而不是
new\u list.append(value)

编辑:以下是一个不带切片的解决方案:

def insert_value(my_list, value, insert_position):
    new_list = []
    if insert_position > length(my_list):
        new_list = my_list
        new_list.append(value)
    elif insert_position < 0:
        new_list = [value]
        for i in range(length(my_list)):
            new_list.append(my_list[i])
    else:
        for i in range(insert_position):
            new_list.append(my_list[i])
        new_list.append(value)
        for i in range(length(my_list)-insert_position):
            new_list.append(my_list[insert_position+i])
    return new_list
def insert_值(我的_列表、值、插入位置):
新列表=[]
如果插入位置>长度(我的列表):
新建列表=我的列表
新建列表。追加(值)
elif插入位置<0:
新列表=[值]
对于范围内的i(长度(我的列表)):
新增列表。追加(我的列表[i])
其他:
对于范围内的i(插入位置):
新增列表。追加(我的列表[i])
新建列表。追加(值)
对于范围内的i(长度(我的列表)-插入位置):
新建\u列表。追加(我的\u列表[插入\u位置+i])
返回新列表

问题出在像
new\u list+value
这样的表达式中,其中
value
实际上是一个整数。对于Python解释器来说,添加两个列表是有意义的,但是添加一个列表和一个整数是无效的。当使用
+
进行连接时,您必须创建一个新的包含
值的单元素列表。有错误的行是一个正确的位置,
append()
。这看起来是一个很好的方法,但不幸的是,我刚刚查看了参数,它说没有切片!所以现在我认为我上面的代码是错误的:(@bonnsaim我的最后一个示例没有使用任何切片。