我想在指定的索引周围交换列表或字符串的两个部分,例如:
([1, 2, 3, 4, 5], 2)
Run Code Online (Sandbox Code Playgroud)
应该回来
[4, 5, 3, 1, 2]
Run Code Online (Sandbox Code Playgroud)
我只应该有一行代码,它适用于字符串,但我得到
只能将列表(不是"int")连接到列表
当我尝试使用列表时.
def swap(listOrString, index):
return (listOrString[index + 1:] + listOrString[index] + listOrString[:index])
Run Code Online (Sandbox Code Playgroud) 我有一个键列表和一个值列表,我想填写一个字典,如下所示:
for key, value in listKeys, listValues:
dict[key] = value
Run Code Online (Sandbox Code Playgroud)
但是,我收到以下错误:
builtins.ValueError: too many values to unpack (expected 2)
Run Code Online (Sandbox Code Playgroud)