列表:["a", "b", "c", "d", "e", "f", "g", "h"]
的Python循环将不会执行inputed最后一次迭代
名单预计:["a", 99, "b", 99, "c", 99, "d", 99, "e", 99, "f", 99, "g", 99, "h"]
列表实际:["a", 99, "b", 99, "c", 99, "d", 99, "e", "f", "g", "h"]
Python代码:
m_o = ["a", "b", "c", "d", "e", "f", "g", "h"]
m = ["a", "b", "c", "d", "e", "f", "g", "h"]
m_expected = ["a", 99, "b", 99, "c", 99, "d", 99, "e", 99, "f", 99, "g", 99, "h"]
num = 99
count = 0
m_l = range(len(m))
for i in m_l:
if i==(1+count):
m.insert(i, num)
count = count + 2
m_l = range(len(m))
#print("1+count : " + str(1+count))
#print(m)
#print("m_l: " + str(m_l))
print("List inputed: ")
print(m_o)
print("\nList expected: ")
print(m_expected)
print("\nList actual: ")
print(m)
'append'好像比这个代码中的insert更好。 –
毛。不要写你自己的功能来做到这一点。 http://stackoverflow.com/q/2407398/139010 –
我刚刚第一次学习Python。在掌握Python基础知识之后,我将使用现有的方法。 –