[英]Inserting elements into list (without using Python's list.insert(index, item) function)
I'm writing a program to insert element into a list at a position before a particular index.我正在编写一个程序,将元素插入到列表中特定索引之前的位置。 Example,
例如,
mylist = [1,2,3,4,5]
enter the index = 3
enter the item = 7
Output:输出:
[1,2,3,7,4,5]
Since the input index is 3, python will append the new item at position index-1 which is 2.由于输入索引为 3,python 将在位置 index-1 附加新项,即 2。
I have tried something like but the output seemed a little off:我试过类似的东西,但输出似乎有点不对劲:
mylist = [1,2,3,4,5]
indexInput = int(input("Enter the index: "))
itemInput = int(input("Enter the item: "))
for i in mylist:
new_slot = indexInput -1
new_length = len(mylist) + 1
if i == new_slot:
mylist[i] = item
mylist.append(mylist[i])
print(mylist)
I know about the insert() function in python but I'm not allowed to use it so i had to code it the long way.我知道 python 中的 insert() 函数,但我不允许使用它,所以我不得不对其进行长时间的编码。 Any help is much appreciated.
非常感谢任何帮助。
You could just use list slicing:你可以只使用列表切片:
mylist = [1,2,3,4,5]
indexInput = int(input("Enter the index: "))
itemInput = int(input("Enter the item: "))
if indexInput<0:
indexInput = len(mylist)+indexInput+1
mylist = mylist[:indexInput] + [itemInput] + mylist[indexInput:]
print(mylist)
# for index 3 and item 7 => [1, 2, 3, 7, 4, 5]
# for index -2 and item 6 => [1, 2, 3, 4, 6, 5]
Explanation:说明:
list[start:end:step] => list items from index [start] to index [end-1] with a step of 1
if start is not given, 0 is assumed, if end is not given, it goes all the way to the end of the list, if step is not given, it assumes a step of 1
In my code I wrote:
mylist = mylist[:indexInput] + [itemInput] + mylist[indexInput:]
for indexInput=3 and itemInput=7
mylist = mylist[:3] + [7] + mylist[3:]
mylist[:3] => mylist from 0 (start not given), to 3-1, step of 1 (step not given)
list1 + list2 => a list that contains elements from list1 and list2
mylist[:3] + [7] => [...elements of mylist[:3]..., 7]
mylist[3:] => mylist from 3, to len(mylist)-1 (end not given), step of 1 (step not given)
If you want to do it the long way, here's a solution:如果你想长期这样做,这里有一个解决方案:
mylist = [1,2,3,4,5]
indexInput = int(input("Enter the index: "))
itemInput = int(input("Enter the item: "))
if indexInput<0:
indexInput = len(mylist)+indexInput+1
leftSide = []
rightSide = []
for i in range(len(mylist)):
if i<indexInput:
leftSide.append(mylist[i])
elif i>indexInput:
rightSide.append(mylist[i])
else:
leftSide.append(itemInput)
leftSide.append(mylist[i])
mylist = leftSide + rightSide
print(mylist)
# for index 3 and item 7 => [1, 2, 3, 7, 4, 5]
# for index -2 and item 6 => [1, 2, 3, 4, 6, 5]
Here is my code这是我的代码
mylist = [1, 2, 3, 4, 5]
indexInput = int(input("Enter the index: "))
itemInput = int(input("Enter the item: "))
for idx, val in enumerate(mylist):
if idx == indexInput:
mylist = mylist[:idx] + [itemInput] + mylist[idx:]
print(mylist)
Hope it can help you, just use enumerate
function to get mylist
index希望它可以帮助你,只需使用
enumerate
函数来获取mylist
索引
list = [1,2,3,4,5]
inputIndex = 3
inputItem = 7
temp = 0
for i in range(len(list)):#continuously swap your elements until you reach the end
if(i == len(list) - 1): # if you're at the end, swap and add the temp to the end
temp = list[i]
list[i] = inputItem
list.append(temp)
elif(i >= inputIndex):
temp = list[i]
list[i] = inputItem
inputItem = temp
print(list)
This is probably what you're looking for, if this is for an assignment where you're not allowed to use insert
then I am assuming you probably aren't allowed to use slicing这可能就是你要找的,如果这是一个你不允许使用
insert
的任务,那么我假设你可能不允许使用切片
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.