python - 在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内置了这个。
mylist.insert(index, element)
推荐阅读
- java - 如何从另一个内部类获取变量以供全局使用
- r - cv.glmnet() 预测与使用“类”和“响应”相反
- python - dict 在视觉工作室(python)中不起作用
- reactjs - 即使在 React Native 中附加数据后,Flatlist 也无法正确呈现数据?
- graphql - ReferenceManyFields(一对多关系)
- java - 首先制作它,以便更大的价值在
- google-kubernetes-engine - 如何通过 GCP Kubernetes Engine 中的 IAP 根据用户 ID 将用户请求路由到特定的 Docker 容器
- angular - 如何使Primeng图表图例的颜色面板为圆形?
- excel - 如何解决空值表达式错误
- pyspark - 获取“StructField”对象在 BinaryClassificationMetrics 上没有属性“_get_object_id”