在python中插入列表 [英] list insertion in python

查看:108
本文介绍了在python中插入列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两个列表,如下所示:-

Suppose I have two lists as follows:-

a = [9,11,12,13] b = [0,5]

现在,我想创建另一个列表,如果索引等于b的任何元素,那么我想在该特定索引处插入-1 因此在上述情况下,如果index = 0,5 新列表将包含[-1,9,11,12,13,-1] 如果上面给出了两个列表作为函数的输入,该怎么做?

Now, I want to create another list in which if the index equals to any element of b,then I want to insert -1 at that particular index so in the above case if index = 0,5 the new list will contain [-1,9,11,12,13,-1] How to do this, if the two lists are given above as input to a function?

推荐答案

如果我理解您的问题,我相信这就是您要寻找的东西.

If I understood your question I believe that's what you're looking for.

a = [1, 2, 3, 4]
b = [0, 3]

def insert_at_indexes(li, indexes, value):
    for ind in indexes:
        li.insert(ind, value)

insert_at_indexes(a,b,-1)

这篇关于在python中插入列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆