简体   繁体   English

我正在尝试使用 insert 方法在列表中已经存在的每个元素之后将相同的元素添加到我的列表中

[英]I am trying to add the same element to my list after every element that is already present in the list using the insert method

mylist = ['.','.','.']
for i in mylist:
    mylist.insert(i,'x')


print(mylist)

The problem is that I keep on getting the error message:问题是我不断收到错误消息:

TypeError: 'str' object cannot be interpreted as an integer

I know of the join() method however I am trying to make it work so that the elements that i am adding can be manipulated later on.我知道 join() 方法,但是我试图让它工作,以便以后可以操作我添加的元素。

When you do for i in mylist you get the actual list items in i , not their indexes.当您for i in mylist执行操作时,您会在i中获得实际的列表项,而不是它们的索引。 You then try to use insert - that functions requires an integer index (and is getting a string item instead).然后您尝试使用insert - 该函数需要 integer 索引(并且正在获取字符串项)。 ( see here ) 见这里

In addition to that, you're iterating over a list while also changing it's length, that's a bad practice and a recipe for a lot of bugs.除此之外,您在迭代列表的同时还更改了它的长度,这是一种不好的做法,并且会导致很多错误。

The correct way would be to create a new list entirely:正确的方法是完全创建一个新列表:

mylist = ['.','.','.']
new_list = []
for item in mylist:
    new_list.append(item)
    new_list.append('x')

Your new_list is now: ['.', 'x', '.', 'x', '.', 'x']你的new_list现在是: ['.', 'x', '.', 'x', '.', 'x']

The issue is insert takes an int and string as a parameter.问题是 insert 将 int 和 string 作为参数。

When you write...当你写...

for i in myList:

You are taking i as the value in each position.您将 i 作为每个 position 中的值。 Since the values in myList are strings, that's the error message you are seeing.由于 myList 中的值是字符串,这就是您看到的错误消息。 Try instead:请尝试:

for i in range(len(myList)):

This returns a list made up of the numbers starting from 0 and going to before len(myList).这将返回一个由从 0 开始到 len(myList) 之前的数字组成的列表。 However, this also presents a problem.然而,这带来了一个问题。 The list of positions is generated before the loop starts, so you have to be very careful since adding to the list doesn't change the number of interations.位置列表是在循环开始之前生成的,因此您必须非常小心,因为添加到列表中不会改变交互次数。 As such, the simplest implementation would be...因此,最简单的实现将是......

i = 1
while i < len(myList):
    myList.insert(i, 'x')
    i += 2

list.insert() method works with indices not the element themselves. list.insert()方法适用于索引而不是元素本身。 When you are saying for i in mylist , you are iterating over the elements, which are strings, so you get the error you mentioned, as indices can not be str .当您for i in mylist ,您正在迭代元素,它们是字符串,因此您会收到您提到的错误,因为索引不能是str First line of thinking should be to go by indices, but then again, if you modify the list while iterating over it, you will modify the list everytime you insert an element and hence will get unexpected results.第一条思路应该是通过索引到 go ,但话又说回来,如果您在迭代列表时修改列表,则每次插入元素时都会修改列表,因此会得到意想不到的结果。 And it is highly discouraged.这是非常不鼓励的。 Nevertheless, if you want to do so, here you go:不过,如果你想这样做,这里是 go:

mylist = ['.','.','.']
for i in range(len(mylist)):
    mylist.insert(i+i+1,'x')

print(mylist)

Output: Output:

['.', 'x', '.', 'x', '.', 'x']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试在使用方法 insert() 时在列表中添加新的最后一个元素 - Trying to add a new last element in a list while using the method insert() 在每 2 个元素之后在列表中插入元素 - insert element in a list after every 2 element 我正在尝试从列表中删除所有其他元素,但它不起作用 - I am trying to delete every other element from a list, it is not working 在每个其他元素列表理解之后在 Python 列表中插入元素 - Insert element in Python list after every other element list comprehension 如何在python列表中的每个其他元素之后插入一个元素 - How to insert an element after every other element in python list 在 Python 列表中每第 n 个元素后插入元素 - Insert element in Python list after every nth element 我试图使用for循环将一个列表的元素插入列表列表中每个列表的第一个元素 - I am trying to use a for-loop to insert an element of one list to the first element of each list within a list of lists 将元素插入列表方法 - Insert element into a list method 用“$”替换每个元素的第一个字符,并在列表中每个元素的末尾添加“_” - Replace the first character of every element with “$” and add “_” at the end of every element present in the list 在list comprehension python中的每个元素后面插入逗号 - insert comma after every element in list comprehension python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM