首页 > 解决方案 > 在python中定义一个add函数

问题描述

我需要帮助在 python 中定义一个 add 函数。这是我第一次用 python 编写,所以我在很多格式上都遇到了麻烦。我需要为 arraylist 定义一个 add 函数,该函数将所有项目 j>=i 移动一个位置,在列表的位置 i 处插入一个元素并增加列表中的元素数量输入: i:索引,即整数非负数最多 n x:对象类型,即任何对象这是我到目前为止所写的:

def add(self, i : int, x : object) :
    if i>0 & i<self.size():

        for n in range(i, self.size())

    else:
        pass 

现在我写了一些伪代码,想知道它是否可行:

def(add(self, i : int, x : object) :
  declare new empty array of size n+1
 if i>0 and i<=n:
   for(i to arr length):
    put i into new array
 else:
  pass

标签: python

解决方案


所以,基本上python内置了这个。

mylist.insert(index, element)

推荐阅读