草庐IT

filter_len

全部标签

python - 使用 len() 和 def __len__(self) : to build a class

只是好奇,在构建类时使用len()或def__len__()有什么区别(优点和缺点)?哪种Python风格最好?classfoo(object):def__init__(self,obs=[])self.data=obsself.max=max(obs)self.min=min(obs)self.len=len(obs)或classfoo(object):def__init__(self,obs=[])self.data=obsself.max=max(obs)self.min=min(obs)def__len__(self):returnlen(self.data)

python - map/filter/reduce 函数序列的干净代码

有没有一种简单的方法可以在一行中编写一系列map/filter/reduce函数?例如代替:reduce(lambdax,y:x*y,filter(lambdax:x>0,map(lambdax:x-1,some_list)))我正在寻找类似的东西:some_list.map(lambdax:x-1,a).filter(lambdax:x>0).reduce(lambdax,y:x*y) 最佳答案 PyFunctional可让您在通过pipinstallPyFunctional安装后做到这一点fromfunctionalimport

python - 'map' 类型的对象在 Python 3 中没有 len()

我在使用Python3时遇到问题。我获得了Python2.7代码,目前我正在尝试更新它。我得到了错误:TypeError:objectoftype'map'hasnolen()在这部分:str(len(seed_candidates))在我这样初始化它之前:seed_candidates=map(modify_word,wordlist)那么,谁能解释一下我必须做什么?(编辑:以前这个代码示例是错误的,因为它使用set而不是map。现在已经更新。) 最佳答案 在Python3中,map返回一个map对象而不是list:>>>L=ma

python - 类型错误 : 'filter' object is not subscriptable

我收到错误消息TypeError:'filter'objectisnotsubscriptable当尝试运行以下代码块时bonds_unique={}forbondinbonds_new:ifbond[0]r_lengthorsheet[ghost_atom][1]>stderr,ghost_atom+1,bond[bond_index],imagebonds_unique[repr(bond)]=bond#Removingduplicatebondsbonds_unique=sorted(bonds_unique.values())和sheet_new=[]bonds_new=[]o

python - Django objects.filter() values_list() vs python list comprehension for __in query

我有一个Django查询集过滤的怪癖(?):ipdb>MagazineIssue.objects.filter(id__in=l_magazines.values_list('id'))Out[0]:[]或ipdb>MagazineIssue.objects.filter(id__in=[l_magazine.idforl_magazineinl_magazines])Out[0]:[]和ipdb>l_magazines.values_list('id')Out[0]:[(1,)]ipdb>[l_magazine.idforl_magazineinl_magazines]Out[0]:

python - 为什么 Pandas 内连接会给出 ValueError : len(left_on) must equal the number of levels in the index of "right"?

我正在尝试将DataFrameA内部连接到DataFrameB并遇到错误。这是我的加入声明:merged=DataFrameA.join(DataFrameB,on=['Code','Date'])这是错误:ValueError:len(left_on)mustequalthenumberoflevelsintheindexof"right"我不确定列顺序是否重要(它们不是真正“有序”的吗?),但以防万一,DataFrame的组织方式如下:DataFrameA:Code,Date,ColA,ColB,ColC,...,ColG,ColH(shape:80514,8-noindex)Da

python 等效于 filter() 获取两个输出列表(即列表的分区)

假设我有一个列表和一个过滤功能。使用类似的东西>>>filter(lambdax:x>10,[1,4,12,7,42])[12,42]我可以得到符合条件的元素。有没有我可以使用的函数来输出两个列表,一个元素匹配,一个剩余元素?我可以调用filter()函数两次,但这有点丑:)编辑:元素的顺序应该保持不变,我可能有多次相同的元素。 最佳答案 试试这个:defpartition(pred,iterable):trues=[]falses=[]foriteminiterable:ifpred(item):trues.append(item

python - 是否需要范围(len(a))?

人们经常在关于SO的python问题中找到这种类型的表达式。要么只是访问可迭代的所有项目foriinrange(len(a)):print(a[i])这只是一种笨拙的写法:foreina:print(e)或分配给可迭代的元素:foriinrange(len(a)):a[i]=a[i]*2应与以下内容相同:fori,einenumerate(a):a[i]=e*2#Orifitisn'ttooexpensivetocreateanewiterablea=[e*2foreina]或用于过滤索引:foriinrange(len(a)):ifi%2==1:continueprint(a[i])

Python:Python 列表是否对 len() 进行计数,还是对每次调用都进行计数?

如果我一直在一个很长的列表上调用len(),我是在浪费时间,还是在后台保留一个int计数? 最佳答案 别担心:它当然会保存计数,因此列表上的len()是一个非常便宜的操作。顺便说一句,字符串、字典和集合也是如此! 关于Python:Python列表是否对len()进行计数,还是对每次调用都进行计数?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/699177/

python - Django QuerySet 上的计数与 len

在Django中,鉴于我有一个QuerySet我要迭代并打印其结果,那么计算对象的最佳选择是什么?len(qs)或qs.count()?(同时考虑到在同一迭代中计算对象不是一种选择。) 最佳答案 虽然Djangodocs推荐使用count而不是len:Note:Don'tuselen()onQuerySetsifallyouwanttodoisdeterminethenumberofrecordsintheset.It'smuchmoreefficienttohandleacountatthedatabaselevel,usingS