python - 向左旋转数组
问题描述
我正在尝试使用以下函数来旋转数组:
def rotLeft(a,d):
temp=[]
temp.append(a[0:-1])
temp.insert(0,a[-1])
return temp
我应该得到输出为 5 1 2 3 4
但我得到 5,[1,2,3,4]
如何解决这个问题呢
解决方案
您必须使用 Use.extend()
而不是.append()
as.append()
和.insert()
is 来添加元素,而.extend()
is 合并两个列表:
def rotLeft(a,d):
temp=[]
temp.extend(a[0:-1])
temp.insert(0,a[-1])
return temp
print(rotLeft([1,2,3,4,5], 1))
输出:
[5, 1, 2, 3, 4]
推荐阅读
- flutter - 使用 Firebase 进行声明式身份验证路由
- python - 在 Python 字典中断言所有值的数据类型的更好方法
- java - 如何从单击的自定义 ListView 项目发送信息?
- javascript - 如何获取放在控制台中的数组对象,带地图的箭头功能不起作用
- pine-script - Pinescript - 3 个级别的确认条件与时间间隔?
- java - 在 recyclerView 中设置 cardView 可见性的问题
- android - Android Firestore Stripe - 添加付款来源
- r - simsurv R 功能:有没有办法定义不同的审查率?
- ios - 在日历函数“date(byAdding: .day, value: , to: )”中,我们给函数的日期是否包含当前日期?
- python-3.x - 无法使用 keras.utils.get_file 下载 txt 文件