Python中文
首页
教程
问答
标签
搜索
登录
注册
罗克布剪刀(Python)枚举列表,根据逻辑提取值,并将这些值与原始lis进行比较
回答此问题可获得
20
贡献值,回答如果被采纳可获得
50
分。
<p>所以问题是有N个人排成一行,玩石头布剪刀。除了我,每个人只能使用一种手,表示为</p> <p><code>['P', 'R', 'S', 'P']</code>表示N只手。你知道吗</p> <p>我可以使用任何类型,并且处于任意位置</p> <p>我需要知道一旦名单到达我的对手是什么位置,所以N-A-1。这意味着我必须反复浏览列表,找出每一场比赛的赢家,并将结果与列表中的下一个位置进行比较,直到找到为止。你知道吗</p> <p>我尝试的是创建一个<code>winners</code>的空数组,然后在给定的列表中枚举。你知道吗</p> <p>一旦有了赢家的值,就会将其与列表枚举中下一个索引的值进行比较。你知道吗</p> <p>我目前还没有解决当我进入最后一个迭代时会发生什么的方法,但是目前我的问题是我得到一个空列表作为优胜者的输出。你知道吗</p> <p>任何关于这种逻辑列表的最佳实践的帮助都是很棒的,我还没有找到任何合适的。你知道吗</p> <p>代码:</p> <pre class="lang-py prettyprint-override"><code>def previous_and_next(n, a, formations): positions = list(formations) print(positions) winners = [] print(winners) for i, pos in enumerate(positions): nex = pos[i + 1] if not winners: if pos == 'R' and nex == 'P': winners.append(nex) elif pos == 'R' and nex == 'S': winners.append(pos) elif pos == 'R' and nex == 'R': continue elif pos == 'P' and nex == 'S': winners.append(nex) elif pos == 'P' and nex == 'R': winners.append(pos) elif pos == 'P' and nex == 'P': continue elif pos == 'S' and nex == 'R': winners.append(nex) elif pos == 'S' and nex == 'P': winners.append(pos) elif pos == 'S' and nex == 'S': continue if nex < len(positions-1): if winners[-1:] == 'P' and nex == 'R' or nex == 'P': continue elif winners[-1:] == 'R' and nex == 'R' or nex == 'S': continue elif winners[-1:] == 'S' and nex == 'P' or nex == 'S': continue elif winners[-1:] == 'P' and nex == 'S': winners.append(pos) elif winners[-1:] == 'R' and nex == 'P': winners.append(pos) elif winners[-1:] == 'S' and nex == 'R': winners.append(pos) def main(): previous_and_next(5,0,'PRSP') main() </code></pre>
0 条评论
分类:
Python问答
请先
登录
后评论
默认排序
时间排序
1 个回答
匿名
1天前
擅长:python、mysql、java
<p>我不是100%确定这个问题在问什么,但您的问题似乎是试图将字符串与int进行比较。请尝试<code>nex = positions[i + 1]</code>而不是<code>nex = i + 1</code>。你知道吗</p>
请先
登录
后评论
针对此问题:
更多的回答
关注
89
关注
收藏
1
收藏,
216
浏览
网友 提问于 2天前
相关Python问题
如何实现一个类,该类在每次更改其属性时更改其“last_edited”变量?
1 回答
如何实现一个类?
5 回答
如何实现一个类的属性设置?
5 回答
如何实现一个能够存储输入并反复访问输入的存储系统?GPA计算器
7 回答
如何实现一个自定义的keras层,它只保留前n个值,其余的都归零?
1 回答
如何实现一个行为类似于Python中序列的最小类?
4 回答
如何实现一个请求的多线程或多处理
10 回答
如何实现一个长时间运行的、事件驱动的python程序?
5 回答
如何实现一个颜色一致的非舔深度地图实时?
1 回答
如何实现一个默认的SQLAlchemy模型类,它包含用于继承的公共CRUD方法?
6 回答
如何实现一次热编码的生成函数
4 回答
如何实现一种在数组中删除对的方法
6 回答
如何实现一类支持向量机用于图像异常检测
4 回答
如何实现一维阵列到二维阵列的复制转换
9 回答
如何实现三维三次样条插值?
5 回答
如何实现三维数据的连接组件标签?
10 回答
如何实现三角形的空间索引
7 回答
如何实现不同模块中对象之间的交互
6 回答
如何实现不同版本的库共存?
3 回答
如何实现不同的班权重
4 回答