[英]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.