相关疑难解决方法(0)

Python中的循环列表迭代器

我需要迭代一个循环列表,可能多次,每次从最后访问的项目开始.

用例是连接池.客户端请求连接,迭代器检查指向的连接是否可用并返回它,否则循环直到找到可用的连接.

有没有一种巧妙的方法在Python中做到这一点?

python iterator list

86
推荐指数
4
解决办法
5万
查看次数

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)

python

5
推荐指数
2
解决办法
991
查看次数

重复数组中的值直到特定长度

我需要某种功能或小技巧来解决我的问题。

所以我得到了一个列表, [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)

python arrays repeat

0
推荐指数
1
解决办法
758
查看次数

标签 统计

python ×3

arrays ×1

iterator ×1

list ×1

repeat ×1