在python中,使用in,获取特定元素
发布时间:2020-12-20 12:26:42 所属栏目:Python 来源:网络整理
导读:我想做这样的事情,但是在 python中: select [from [list] where [index at index n] =’hello’ 到目前为止,我有这个 cells = [' ','hello',' ',' '] emptySpots = [] for s in range(len(cells)): if cells[s] == 'hello': emptySpots.append(s) 这给了我
我想做这样的事情,但是在
python中:
select [from [list] where [index at index n] =’hello’ 到目前为止,我有这个 cells = [' ','hello',' ',' '] emptySpots = [] for s in range(len(cells)): if cells[s] == 'hello': emptySpots.append(s) 这给了我一个单元格的索引列表,其中包含’hello’,但我打赌这是一种更直接(pythonesque)的方法来做到这一点. 什么是好的是一个单行,只返回单元格中等于”的元素数量的计数. 解决方法
你的问题很混乱.您的代码不会获取空单元格的索引,而是获取元素hello的索引.
看来你有两个问题在一个? 如果你真的想得到你在最后一句话中声称的“空单元格”的数量,你可以使用list.count: empty_spots = cells.count(' ') (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |