我试着做出有顺序选择的自动响应。

choice = ['welcome','welcome back','nice to see you','hai dude']
>>> welcome
>>> welcome back
>>> nice to see you
>>> hai dude
>>> welcome
>>> welcome back
>>> nice to see you
>>> hai dude

在使用random.choice()之前,如何从“welcome”到“hai dude”再到“walcome”进行序列选择,但现在我需要序列选择,有人能给我建议吗???

最佳答案

您可以通过导入from itertools import cycle来使用itertools.cycle

In [3]: c=cycle(['welcome','welcome back','nice to see you','hai dude'] )

In [4]: next(c)
Out[4]: 'welcome'

In [5]: next(c)
Out[5]: 'welcome back'

In [6]: next(c)
Out[6]: 'nice to see you'

In [7]: next(c)
Out[7]: 'hai dude'

In [8]: next(c)
Out[8]: 'welcome'

In [9]: next(c)
Out[9]: 'welcome back'

更新
from itertools import cycle
c=cycle(['welcome','welcome back','nice to see you','hai dude'] )
print next(c)
print next(c)

next(c)将连续给出下一个元素。

关于python - 如何在Python中进行序列选择,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32134195/

10-12 00:04