python - 遍历一个没有 double for 的列表
问题描述
我正在尝试像下面的代码一样遍历一个列表,但我在 python 中有点新,我想知道是否有机会克服这两个 for 循环。有什么办法吗?提前致谢
temp = [0, 2, 3, 4]
for index, pointer in enumerate(temp):
for i in range(len(temp)):
if i != index:
print(temp[i])
结果 :
2
3
4
0
3
4
0
2
4
0
2
3
解决方案
temp = [0, 2, 3, 4]
sol=[]
for i in range(len(temp)):
sol.extend(temp[:i]+temp[i+1:])
print(sol)
输出
[2, 3, 4, 0, 3, 4, 0, 2, 4, 0, 2, 3]
推荐阅读
- vpn - 如何通过 Python 连接 PPTP?
- java - Java Swing 绘制一个矩形
- reactjs - 当数据库中的数据发生变化时,更新我的反应状态的最佳方法是什么?
- sockets - 避免插座接触期间 ESP8266 零星挂起
- date - PySpark 字符串列到时间戳的转换
- c# - Razor 组件接口
- c++ - C++Builder 10.4/11:帮助想要解决编译 64 位 Windows 应用程序时出现的致命错误“Invalid object file 'altsvc.o'”
- postgresql - posgreSQL 如何标记冻结的行?
- github - 如何通过代码行、依赖项和版本信息搜索 github 存储库?
- c - C语言中的Luhn算法;使用atoi函数调试问题