相关疑难解决方法(0)

如何在python中找到"过滤器"对象的长度

>>> n = [1,2,3,4]

>>> filter(lambda x:x>3,n)
<filter object at 0x0000000002FDBBA8>

>>> len(filter(lambda x:x>3,n))
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    len(filter(lambda x:x>3,n))
TypeError: object of type 'filter' has no len()
Run Code Online (Sandbox Code Playgroud)

我无法得到我得到的清单的长度.所以我尝试将它保存到变量中,就像这样......

>>> l = filter(lambda x:x>3,n)
>>> len(l)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    len(l)
TypeError: object of type 'filter' has no len()
Run Code Online (Sandbox Code Playgroud)

而不是使用循环,有没有办法得到这个的长度?

filter variable-length python-3.x

48
推荐指数
5
解决办法
4万
查看次数

Google Python风格指南

为什么Google Python样式指南更喜欢列表推导和for循环而不是filter,map和reduce?

不推荐使用的语言功能:..."使用列表推导和循环而不是过滤,映射和减少."

给出的解释是:"我们不使用任何不支持这些功能的Python版本,因此没有理由不使用新的样式."

python

15
推荐指数
3
解决办法
7590
查看次数

忽略字符串比较中的大小写

如果我有两个变量,a和b,它们可以是整数,浮点数或字符串.

True如果它们相等,我想返回(如果是字符串,则忽略大小写).

尽可能像Pythonic.

python equals

9
推荐指数
2
解决办法
4万
查看次数

How to search a list with nested dictionary by dictionary value, returning the index of the list with the dictionary element

If I have a list with some nested dictionaries each containing the same keyset and value type set.

list = [{'a': 1, 'b': 2.0, 'c': 3.0}, 
        {'a': 4, 'b': 5.0, 'c': 6.0}, 
        {'a': 7, 'b': 8.0, 'c': 9.0}]
Run Code Online (Sandbox Code Playgroud)

返回 '​​b' 字典键中 5.0 第一次出现的列表索引的最 Pythonic 方法是什么,如果没有找到,则返回 None ?

我知道我可以手动迭代和搜索:

#with the list above...

result = None
for dict in list:
  if dict['b'] == 5.0:
    result = list.index(dict)
    break
print result
#(prints 1)


#as another example, same code but searching for a …
Run Code Online (Sandbox Code Playgroud)

python dictionary list

3
推荐指数
1
解决办法
191
查看次数

如何使用python脚本将Mysql表数据导出到excel文件?

我一直在尝试使用python 2.7.8将数据从表导出到excel文件,但我还没有成功.请帮我.我的要求是从表中导出数据并将其存储在本地(Windows C驱动器)中.

#!/usr/bin/python
import smtplib
import base64
import os
import sys
import xlswriter
import xlwt
import datetime
import MySQLdb
from pyh import *
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
db = MySQLdb.connect("192.168.1.118","stp","stp","STP")
cursor = db.cursor()
query = ("""select * from stp_automation_output""")
cursor.execute(query)
myresults = cursor.fetchall()
workbook = xlwt.Workbook()
worksheet = workbook.add_sheet("My Sheet")
#date_format = workbook.add_format({'num_format': 'd mmmm yyyy'})
bold = workbook.add_format({'bold': 1})
worksheet.write('A1','Sno',bold)
worksheet.write('B1','function_name',bold)
worksheet.write('C1','input1',bold)
worksheet.write('D1','input2',bold)
worksheet.write('E1','input3',bold)
worksheet.write('F1','Expected_output',bold)
worksheet.write('G1','Actual_output',bold)
worksheet.write('H1','Result',bold)
row = 1
col = 0
for …
Run Code Online (Sandbox Code Playgroud)

python-2.7

2
推荐指数
1
解决办法
2万
查看次数

从一系列词典中返回一个特定的词典

如果我有以下代码

attributes = []

attributes.append({'attribute': 'noir', 'group': 'coloris', 'id': '8'})
attributes.append({'attribute': 's', 'group': 'taille_textile', 'id': '29'})
attributes.append({'attribute': 'm', 'group': 'taille_textile', 'id': '24'})
attributes.append({'attribute': 'l', 'group': 'taille_textile', 'id': '25'})
attributes.append({'attribute': 'xl', 'group': 'taille_textile', 'id': '26'})
Run Code Online (Sandbox Code Playgroud)

我想返回列表中包含某个id的对象,最好的方法是什么?

我知道一个解决方案是使用这样的for循环

def getItemById(id):
    for i in attributes:
        for k,v in i.items():
            if (k == 'id' and v == id):
                return i
Run Code Online (Sandbox Code Playgroud)

我敢肯定除此之外必须有更优雅或更有效的方法吗?

这里有机会使用lambdas吗?这会带来性能上的好处吗?

python lambda dictionary list object

2
推荐指数
1
解决办法
233
查看次数

从字典列表中获取特定字典的值

假设我有以下字典列表:

months = [
    {'id':'001','date':'January'},
    {'id':'002','date':'February'},
    {'id':'003','date':'March'},
    {'id':'004','date':'April'},
    {'id':'005','date':'May'},
    {'id':'006','date':'June'},
    {'id':'007','date':'July'},
    {'id':'008','date':'August'},
    {'id':'009','date':'September'},
    {'id':'010','date':'October'},
    {'id':'011','date':'November'},
    {'id':'012','date':'December'},
]
Run Code Online (Sandbox Code Playgroud)

如果用户输入月份为一月,他应该得到的 ID 为 001。

我试过这个,但它只返回月份列表。

res = [ mon['date'] for mon in months]
Run Code Online (Sandbox Code Playgroud)

我需要直接来自密钥本身的 id。我怎样才能做到这一点?

python python-3.x

-3
推荐指数
1
解决办法
88
查看次数