英文:
How to add words/strings in lines from a list?
问题
I'm trying to add different words from a list to lines in order. However, I am stuck with it. Can you please tell me how to proceed? I am getting a type error, and couldn't get my output.
The output that I'm trying to get:
it starts with: one or more.
it starts with: two or more.
it starts with: three or more.
The code that I tried:
a=["one","two","three"]
for i in a:
print("it starts with: " + a[i] + "or more")
The result I'm getting:
TypeError: list indices must be integers or slices, not str
英文:
I'm trying to add different words from a list to lines in order. However, I am stuck with it. Can you please tell me how to proceed? I am getting a type error, and couldn't get my output.
The output that I'm trying to get:
it starts with: one or more.
it starts with: two or more.
it starts with: three or more.
The code that I tried:
a=["one","two","three"]
for i in a:
print("it starts with: " + a[i] + "or more")
The result I'm getting:
TypeError: list indices must be integers or slices, not str
答案1
得分: 4
i
已经是你想要的元素。
在Python中,表达式for i in a
将数组a
的元素分配给循环的每个迭代中的变量i
。你不需要访问元素。
for i in a:
print("它以以下内容开头:" + i + "或更多")
英文:
i
is already the element you want.
In Python the expression for i in a
assigns elements of the array a
to the variable i
on every pass of the loop. You don't nedd to address the elements.
for i in a:
print("it starts with: " + i + "or more")
答案2
得分: 2
所以你遇到的问题是,当你告诉Python从列表中取一个元素i
时,i
的值不是索引,而是值。所以基本上你的代码在做的是a["one"]
,这是行不通的。
如果你将a[i]
替换为i
,它应该可以工作。另外,我建议将+
替换为,
,这样你的两个字符串和从a
中取出的值之间会有一个空格。
英文:
So the issue you're encountering is that when you tell python to take an element i
from the list the i
value is not the index but rather the value. So basically what your code is doing is a["one"]
which doesn't work.
If you replace a[i]
with just i
it should work. Also I would recommend replacing '+' with ',' for there to be a space between your two strings and the value you're taking from a.
答案3
得分: 2
已经表示列表中的每个元素。您可以使用f字符串将其放入字符串中:
for i in a:
print(f"它以{i}或更多开头")
英文:
i already represents each element in the list. You can use an f-string to put it into the string:
for i in a:
print(f"it starts with: {i} or more")
答案4
得分: 1
You mix-up the iteration part, here looping over the length of the list
a = ["one", "two", "three"]
for i in range(len(a)):
print("it starts with: " + a[i] + " or more")
Here another approach. Used a string template where {}
represent the placeholder.
a = ["one", "two", "three"]
template = "it starts with: {} or more"
print(*map(template.format, a), sep='\n')
英文:
You mix-up the iteration part, here looping over the length of the list
a=["one","two","three"]
for i in range(len(a)):
print("it starts with: " + a[i] + "or more")
Here another approach. Used a string template where {}
represent the placeholder.
a=["one","two","three"]
template = "it starts with: {} or more"
print(*map(template.format, a), sep='\n')
答案5
得分: 1
for循环与可迭代对象一起工作
所以,例如,
for X in Y:
pass
这里,Y是一个可迭代对象,X将逐一从Y获取值,直到可迭代对象耗尽。
在你的情况下,a是一个字符串列表(可迭代对象)。
因此,你可以简单地这样做:
a = ["one", "two", "three"]
for w in a:
print('it starts with:', w, 'or more')
请注意,在这种情况下,不需要使用f-字符串或字符串连接。这是因为可以将多个值传递给print(),它们将默认以单个空格分隔。
输出:
it starts with: one or more
it starts with: two or more
it starts with: three or more
英文:
for loops work with iterables
So, for example,
for X in Y:
pass
Here, Y is an iterable and X will acquire the values from Y one at a time until the iterable is exhausted.
In your case, a is a list (an iterable) of strings.
Therefore you could simply do this:
a = ["one", "two", "three"]
for w in a:
print('it starts with:', w, 'or more')
Note how neither f-strings nor string concatenation is required in this case. This is because multiple values can be passed to print() and they will be emitted separated by a single space (by default).
Output:
it starts with: one or more
it starts with: two or more
it starts with: three or more
答案6
得分: 1
我认为其他答案应该会解决你的问题,但为了避免将来出现这种微不足道的错误,你应该尝试使用以下方法检查你正在使用的变量的值:
- 添加一个
print()
语句。在你的情况下,在第3行之后添加这行代码:
print(i)
- 使用你的集成开发环境的调试功能。例如,在VSCOde中:https://code.visualstudio.com/docs/python/debugging
英文:
I think the other answers should fix your problem but to avoid this trivial error in the future, you should try to examine the value of the variable you're using with one of these methods:
- Adding a
print()
statement. In your case: add this after line 3
print(i)
- Using debugging feature of your IDE. For example in VSCOde: https://code.visualstudio.com/docs/python/debugging
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论