I am trying to use the Python code below to output a new list. The output of print (words) should be ['my','name','is','michael','apples','i','like','cars']
.
我试图使用下面的Python代码输出一个新的列表。 print(单词)的输出应该是['my','name','is','michael','apples','i','like','cars']。
Right now, print (words)
outputs only ['cars']
. What am I missing here?
现在,打印(单词)仅输出['cars']。我在这里错过了什么?
a = 'my name is michael and i like cars'
b = a.split()
words = None
for i, j in enumerate(b):
words = []
if j == "and" and b[i+1][0] == "i":
words.append("apples")
else:
words.append(j)
print (words)
5 个解决方案
#1
Create words
outside the loop, you only see the last word because you keep setting words equal to an empty list each iteration:
在循环外创建单词,您只看到最后一个单词,因为您在每次迭代时都会将单词设置为等于空列表:
words = [] # outside the loop
for i, j in enumerate(b):
If and
happens to be the last word you will also get an IndexError
. You can set the start index to 1
in enumerate then you don't need to +1 and will avoid any potential error indexing:
如果恰好是最后一个词,你也会得到一个IndexError。您可以在枚举中将起始索引设置为1,然后您不需要+1并且将避免任何潜在的错误索引:
words = []
for i, j in enumerate(b, 1):
if j == "and" and b[i][0] == "i":
You can put it all in a list comprehension:
你可以把它全部放在列表理解中:
a = 'my name is michael and i like cars'
b = a.split()
words = ["apples" if wrd == "and" and b[i][0] == "i" else wrd for i, wrd in enumerate(b,1)]
print(words)
['my', 'name', 'is', 'michael', 'apples', 'like', 'cars']
You can also avoid indexing using iter
and next
:
您还可以使用iter和next来避免索引:
a = 'my name is michael and i like cars'
it = iter(a.split())
words = ["apples" if wrd == "and" and next(it," ")[0] == "i" else wrd for wrd in it ]
print(words)
['my', 'name', 'is', 'michael', 'apples', 'like', 'cars']
#2
You have an issue in your loop. Every time you iterate, you reset words
to []
您的循环中存在问题。每次迭代时,都会将单词重置为[]
Set the list to []
outside of the for loop, like so:
将列表设置为for循环外的[],如下所示:
words = []
for i, j in enumerate(b):
if j == "and" and b[i+1][0] == "i":
words.append("apples")
else:
words.append(j)
This should work.
这应该工作。
#3
TigerhawkT3 is correct. To fix your code, instead of initializing words to None, try initializing it as an empty array:
TigerhawkT3是正确的。要修复代码,请尝试将其初始化为空数组,而不是将单词初始化为None:
a = 'my name is michael and i like cars'
b = a.split()
words = []
for i, j in enumerate(b):
if j == "and" and b[i+1][0] == "i":
words.append("apples")
else:
words.append(j)
print (words)
#4
This should work for you.
这应该适合你。
a = 'my name is michael and i like cars'
b = a.split()
words = []
for x in range(len(b)-1):
if b[x] == "and" and b[x+1] == "i":
words.append("apples")
else:
words.append(j)
print (words)
#5
I have a more compact method.
我有一个更紧凑的方法。
I think it's more readable and easy to understand. You can refer as below:
我认为它更易读,更容易理解。你可以参考如下:
This is your var I delcare response:
这是你的var I delcare回复:
a = 'my name is michael and i like cars'
b = a.split()
words = []
for i, j in enumerate(b):
if j == "and" and b[i+1][0] == "i":
words.append("apples")
else:
words.append(j)
print (words)
#1
Create words
outside the loop, you only see the last word because you keep setting words equal to an empty list each iteration:
在循环外创建单词,您只看到最后一个单词,因为您在每次迭代时都会将单词设置为等于空列表:
words = [] # outside the loop
for i, j in enumerate(b):
If and
happens to be the last word you will also get an IndexError
. You can set the start index to 1
in enumerate then you don't need to +1 and will avoid any potential error indexing:
如果恰好是最后一个词,你也会得到一个IndexError。您可以在枚举中将起始索引设置为1,然后您不需要+1并且将避免任何潜在的错误索引:
words = []
for i, j in enumerate(b, 1):
if j == "and" and b[i][0] == "i":
You can put it all in a list comprehension:
你可以把它全部放在列表理解中:
a = 'my name is michael and i like cars'
b = a.split()
words = ["apples" if wrd == "and" and b[i][0] == "i" else wrd for i, wrd in enumerate(b,1)]
print(words)
['my', 'name', 'is', 'michael', 'apples', 'like', 'cars']
You can also avoid indexing using iter
and next
:
您还可以使用iter和next来避免索引:
a = 'my name is michael and i like cars'
it = iter(a.split())
words = ["apples" if wrd == "and" and next(it," ")[0] == "i" else wrd for wrd in it ]
print(words)
['my', 'name', 'is', 'michael', 'apples', 'like', 'cars']
#2
You have an issue in your loop. Every time you iterate, you reset words
to []
您的循环中存在问题。每次迭代时,都会将单词重置为[]
Set the list to []
outside of the for loop, like so:
将列表设置为for循环外的[],如下所示:
words = []
for i, j in enumerate(b):
if j == "and" and b[i+1][0] == "i":
words.append("apples")
else:
words.append(j)
This should work.
这应该工作。
#3
TigerhawkT3 is correct. To fix your code, instead of initializing words to None, try initializing it as an empty array:
TigerhawkT3是正确的。要修复代码,请尝试将其初始化为空数组,而不是将单词初始化为None:
a = 'my name is michael and i like cars'
b = a.split()
words = []
for i, j in enumerate(b):
if j == "and" and b[i+1][0] == "i":
words.append("apples")
else:
words.append(j)
print (words)
#4
This should work for you.
这应该适合你。
a = 'my name is michael and i like cars'
b = a.split()
words = []
for x in range(len(b)-1):
if b[x] == "and" and b[x+1] == "i":
words.append("apples")
else:
words.append(j)
print (words)
#5
I have a more compact method.
我有一个更紧凑的方法。
I think it's more readable and easy to understand. You can refer as below:
我认为它更易读,更容易理解。你可以参考如下:
This is your var I delcare response:
这是你的var I delcare回复:
a = 'my name is michael and i like cars'
b = a.split()
words = []
for i, j in enumerate(b):
if j == "and" and b[i+1][0] == "i":
words.append("apples")
else:
words.append(j)
print (words)