【文件属性】:
文件名称:leetcode卡-Python-Common-Mistakes:我作为Python初学者犯的一些常见错误
文件大小:1KB
文件格式:ZIP
更新时间:2021-07-06 20:56:01
系统开源
leetcode卡Python-常见错误
我作为
Python
初学者犯的一些常见错误
索引和for循环
我的目标是获取列表中所有“1”的索引。
List
=
[1,0,1,1,0,1]
[List.index(x)
for
x
in
List
if
x
==
1]
输出:[0,
0,
0,
0]
'List.index(x)'
查找列表中第一个
'1'
的索引。
因此它只返回上面列表中第一个“1”的索引。
使固定:
[i
for
i
in
range(len(List))
if
List[i]
==
0]
输出:[1,
4]
IndexError:列表索引超出范围
示例:合并排序数组
()
测试用例:
nums1
=
[1,2,3,0,0,0]
m
=
3
nums2
=
[2,5,6]
n
=
3
代码:
for
i
in
range(n):
j
=
i
while
nums1[j]
<
=
nums2[i]:
j
+=
1
print(
"
j
"
,j)
nums1.insert(j,nums2[i])
nums1.pop
()
输出:IndexError:列表索引超出范围
如
【文件预览】:
Python-Common-Mistakes-master
----README.md(2KB)