Python 如何在“开始”大于“停止”的情况下对列表进行切片
我想从开始到结束对列表进行切片,但让迭代继续经过最后一个元素,并一直循环到第一个元素。然后继续走,直到停下来 我尝试过列表切片,如下所示:Python 如何在“开始”大于“停止”的情况下对列表进行切片,python,Python,我想从开始到结束对列表进行切片,但让迭代继续经过最后一个元素,并一直循环到第一个元素。然后继续走,直到停下来 我尝试过列表切片,如下所示: list_a = list(range(8)) reordered_list = list_a[5:2] 但只收到一个空列表: [] 理想情况下,我希望打印出列表a: [5, 6, 7, 0, 1] 基本上,我希望遍历进行“完整循环”,并在索引0处继续迭代 def parse(input, start, stop):
list_a = list(range(8))
reordered_list = list_a[5:2]
但只收到一个空列表:
[]
理想情况下,我希望打印出列表a:
[5, 6, 7, 0, 1]
基本上,我希望遍历进行“完整循环”,并在索引0处继续迭代
def parse(input, start, stop):
partial_list = input[:stop]
return partial_list[start:] + partial_list[:start]
full_list = range(8)
parse(full_list, 2, 5) # Outputs [2, 3, 4, 0, 1]
编辑:你的问题有些不一致,如果可能的话请重写
编辑:您的问题中存在一些不一致之处,如果可能,请重写。当您给出一个切片索引时,如-
list_a[5:2]
它将采取步骤1,因为开始是在停止之后,这些索引之间没有元素(切片不会自行循环)
您应该使用模
(余数)运算符和for循环或列表理解来获得所需的列表。范例-
def foo(list_a, start, stop):
if start >= len(list_a):
start = len(list_a) - 1
if stop >= len(list_a):
stop = len(list_a) - 1
i = start
ret = []
while i != stop:
ret.append(list_a[i])
i = (i+1)%len(list_a)
return ret
示例/演示-
>>> def foo(list_a, start, stop):
... if start >= len(list_a):
... start = len(list_a) - 1
... if stop >= len(list_a):
... stop = len(list_a) - 1
... i = start
... ret = []
... while i != stop:
... ret.append(list_a[i])
... i = (i+1)%len(list_a)
... return ret
...
>>> foo(list(range(8)),5,2)
[5, 6, 7, 0, 1]
当你给出一个切片索引,比如-
list_a[5:2]
它将采取步骤1,因为开始是在停止之后,这些索引之间没有元素(切片不会自行循环)
您应该使用模
(余数)运算符和for循环或列表理解来获得所需的列表。范例-
def foo(list_a, start, stop):
if start >= len(list_a):
start = len(list_a) - 1
if stop >= len(list_a):
stop = len(list_a) - 1
i = start
ret = []
while i != stop:
ret.append(list_a[i])
i = (i+1)%len(list_a)
return ret
示例/演示-
>>> def foo(list_a, start, stop):
... if start >= len(list_a):
... start = len(list_a) - 1
... if stop >= len(list_a):
... stop = len(list_a) - 1
... i = start
... ret = []
... while i != stop:
... ret.append(list_a[i])
... i = (i+1)%len(list_a)
... return ret
...
>>> foo(list(range(8)),5,2)
[5, 6, 7, 0, 1]
使用模数运算符,
%
:
>>> l = list(range(8))
>>> start = 5
>>> while start%len(l) != 2:
... print(l[start%len(l)])
... start += 1
...
5
6
7
0
1
作为一项功能:
>>> def foo(l, start, end):
... result = []
... while start%len(l) != end:
... result.append(l[start%len(l)])
... start += 1
... return result
...
>>> foo(l, 5, 2)
[5, 6, 7, 0, 1]
使用模数运算符,
%
:
>>> l = list(range(8))
>>> start = 5
>>> while start%len(l) != 2:
... print(l[start%len(l)])
... start += 1
...
5
6
7
0
1
作为一项功能:
>>> def foo(l, start, end):
... result = []
... while start%len(l) != end:
... result.append(l[start%len(l)])
... start += 1
... return result
...
>>> foo(l, 5, 2)
[5, 6, 7, 0, 1]
这样做:
def foo(list_a, start, stop):
if (start <= stop):
return list_a[start:stop]
else:
return list_a[start:] + list_a[:stop]
这样做:
def foo(list_a, start, stop):
if (start <= stop):
return list_a[start:stop]
else:
return list_a[start:] + list_a[:stop]
模算子是你的朋友。每当您请求列表索引时,请请求[index]mod[size of list]
MyList[i%len(Mylist)]
在您的情况下,写一个检查,如果结束索引小于开始索引,将列表的大小添加到结束索引,然后使用我建议的代码。模数运算符是您的朋友。每当您请求列表索引时,请请求[index]mod[size of list]
MyList[i%len(Mylist)]
在您的情况下,请编写一个检查,如果结束索引小于开始索引,请将列表的大小添加到结束索引,然后使用我建议的代码。这对我很有用:
>>> reordered = (list_a[5:]+list_a[:2])
>>> print (reordered)
>>> [5, 6, 7, 0, 1]
基本上是切片,然后将它们添加到一起。这对我来说很有效:
>>> reordered = (list_a[5:]+list_a[:2])
>>> print (reordered)
>>> [5, 6, 7, 0, 1]
基本上是切片,然后将它们添加到一起。谢谢!我喜欢你简洁的回答:)谢谢!我喜欢你简洁的回答:)