如何在Python中进行序列选择

2024-10-03 15:28:39 发布

您现在位置:Python中文网/ 问答频道 /正文

我试着让自动响应有一个序列选择。你知道吗

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  

在我使用之前,如何从“欢迎”到“hai dude”再到“walcome”进行顺序选择随机选择()但是现在我需要序列选择,有人能给我建议吗???你知道吗


Tags: toyou顺序back序列建议nicesee
1条回答
网友
1楼 · 发布于 2024-10-03 15:28:39

您可以通过导入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)将连续给出下一个元素。你知道吗

相关问题 更多 >