我有一个生成系列的生成器,例如:
def triangleNums():
'''generate series of triangle numbers'''
tn = 0
counter = 1
while(True):
tn = tn + counter
yield tn
counter = counter + 1
Run Code Online (Sandbox Code Playgroud)
在python 2.6中,我可以进行以下调用:
g = triangleNums() # get the generator
g.next() # get next val
Run Code Online (Sandbox Code Playgroud)
但是在3.0中,如果我执行相同的两行代码,我会收到以下错误:
AttributeError: 'generator' object has no attribute 'next'
Run Code Online (Sandbox Code Playgroud)
但是,循环迭代器语法在3.0中有效
for n in triangleNums():
if not exitCond:
doSomething...
Run Code Online (Sandbox Code Playgroud)
我还没有能找到解释3.0行为差异的任何东西.
如何重复列表的每个元素n
次数并形成新列表?例如:
x=[1,2,3,4]
n=3
x1=[1,1,1,2,2,2,3,3,3,4,4,4]
Run Code Online (Sandbox Code Playgroud)
x*n
不起作用
for i in x[i]
x1=n*x[i]
Run Code Online (Sandbox Code Playgroud)
必须有一个简单而聪明的方式.
重复列表最大元素长度的最有效方法是什么?
拿这个:
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) 所以我有一个列表[a,b,c]
并且我想获取[a,b,c,a,b,c,...a,b,c]
.
我当然可以用两个嵌套循环来做到这一点,但一定有更好的方法吗?itertools.cycle()
如果我能提供一个计数,这将是解决方案。
两个限制:
我想将一个小数组复制到特定长度的数组
例:
var = [22,33,44,55] # ==> len(var) = 4
n = 13
Run Code Online (Sandbox Code Playgroud)
我想要的新数组是:
var_new = [22,33,44,55,22,33,44,55,22,33,44,55,22]
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
import numpy as np
var = [22,33,44,55]
di = np.arange(13)
var_new = np.empty(13)
var_new[di] = var
Run Code Online (Sandbox Code Playgroud)
我收到错误消息:
DeprecationWarning:分配将在将来引发错误,最有可能是因为索引结果形状与值数组形状不匹配。您可以
arr.flat[index] = values
用来保留旧的行为。
但是我得到了相应的变量:
var_new
array([ 22., 33., 44., 55., 22., 33., 44., 55., 22., 33., 44.,
55., 22.])
Run Code Online (Sandbox Code Playgroud)
那么,如何解决错误呢?还有其他选择吗?
您好,我正在尝试一个简单的密码程序,该程序将按用户输入的值移动字母,但是,当该单词包含字母“z”时,它将抛出索引超出范围的错误。因此,为了解决这个问题,我尝试了复制列表的简单方法,但我想使用更好的方法来做到这一点。我想检查索引是否超出范围,然后再次循环列表并执行与代码中所示相同的任务非常感谢!
alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
direction = input("Type 'encode' to encrypt, type 'decode' to decrypt:\n")
text = input("Type your message:\n").lower()
shift = int(input("Type the shift number:\n"))
def encrypt(plain_text, shift_amount):
cipher_word = ""
indexed_letter = len(alphabet)
for letter in plain_text:
position = alphabet.index(letter)
new_position = position + shift_amount
new_letter = alphabet[new_position]
cipher_word += new_letter
if …
Run Code Online (Sandbox Code Playgroud) 我正在研究一个项目,我需要在列表中重复一定次数的列表.显然,L.append(L)只是再次添加元素而不创建单独的列表.我只是对如何在大列表中分开列表感到困惑.
简而言之,这就是我所拥有的:
L = [1,2,3,4,5]
Run Code Online (Sandbox Code Playgroud)
如果我想重复一遍,比如说3次,那么我有:
L = [[1,2,3,4,5],[1,2,3,4,5],[1,2,3,4,5]]
Run Code Online (Sandbox Code Playgroud)
我该如何实现这一目标?我正在寻找大名单中的列表.
提前致谢.
有没有办法创建迭代器来重复列表中的元素某些时间?例如,列出了一个列表:
color = ['r', 'g', 'b']
Run Code Online (Sandbox Code Playgroud)
有没有办法以itertools.repeatlist(color, 7)
可以生成以下列表的形式创建迭代器?
color_list = ['r', 'g', 'b', 'r', 'g', 'b', 'r']
Run Code Online (Sandbox Code Playgroud) 对于像 的列表['A', 'B', 'C']
,我试图构建或找到一个函数,该函数将在每次调用时返回列表的下一个元素。在这种情况下,如果它被调用超过 3 次,A
则将被返回。在那之后B
等等。
我有一个['A', 'B', 'C']
要应用于不同对象的颜色列表。每次运行代码时,对象的数量可能会有所不同。如果列表中的对象多于颜色,我希望第四个对象具有 color 'A'
。所以,正如标题所说,我想遍历一个列表 by 和 index 并从头开始 if index > len(list)
。以下自定义函数looper
将做到这一点。
def looper(lst, runs):
j = 0
for i in range(0, (len(lst)+(runs-(len(lst))))):
if i < len(lst):
print(lst[i])
else:
print(lst[j])
j +=1
Run Code Online (Sandbox Code Playgroud)
# input:
runs = 2
looper(['A', 'B', 'C'], runs)
# output:
A
B
Run Code Online (Sandbox Code Playgroud)
#input :
runs = 5
looper(['A', 'B', 'C'], runs)
#output …
Run Code Online (Sandbox Code Playgroud) 发电机一旦使用过一次,就无法再次使用。为什么是这样?
考虑以下代码:
def generator(n):
a = 1
for _ in range(n):
yield a
a += 1
def run_generator(generator):
for a in generator:
print(a, end = " ")
Run Code Online (Sandbox Code Playgroud)
如果我要执行这个:
count_generator = generator(10)
run_generator(count_generator)
run_generator(count_generator)
Run Code Online (Sandbox Code Playgroud)
它只会打印:
1 2 3 4 5 6 7 8 9 10
Run Code Online (Sandbox Code Playgroud)
基本上,生成器在执行一次后就会死亡。
我知道生成器只能使用一次这一事实是 Python 内置的东西,但为什么会这样呢?是否有特定原因只允许生成器对象执行一次?
python ×11
list ×2
python-3.x ×2
generator ×1
iteration ×1
iterator ×1
mutable ×1
nested-lists ×1
numpy ×1
object ×1
python-2.7 ×1
replication ×1