我需要迭代一个循环列表,可能多次,每次从最后访问的项目开始.
用例是连接池.客户端请求连接,迭代器检查指向的连接是否可用并返回它,否则循环直到找到可用的连接.
有没有一种巧妙的方法在Python中做到这一点?
重复列表最大元素长度的最有效方法是什么?
拿这个:
list = ['one', 'two', 'three']
max_length = 7
Run Code Online (Sandbox Code Playgroud)
并产生这个:
final_list = ['one', 'two', 'three', 'one', 'two', 'three', 'one']
Run Code Online (Sandbox Code Playgroud) 我需要某种功能或小技巧来解决我的问题。
所以我得到了一个列表,
[1,2,3,4]
但我需要这个数组更长,重复相同的元素,所以假设我需要一个长度为 10 的数组,所以它变成:
[1,2,3,4,1,2,3,4,1,2]
所以我需要以相同的顺序使用与列表中相同的值来扩展列表
returnString = the array or string to return with extended elements
array = the basic array which needs to be extended
length = desired length
Run Code Online (Sandbox Code Playgroud)
编辑:
returnString = ""
array = list(array)
index = 0
while len(str(array)) != length:
if index <= length:
returnString += array[index]
index += 1
else:
toPut = index % length
returnString.append(array[toPut])
index += 1
return returnString
Run Code Online (Sandbox Code Playgroud)