我正在编写一个脚本,我必须根据许多条件测试数字.如果满足任何条件,我想返回True,我想以最快的方式做到这一点.
我的第一个想法是使用any()而不是嵌套if语句或多个or链接我的条件.因为我会满意,如果任何条件是True我真的可以从any()懒惰和尽快返回真正受益.
基于以下事实:以下打印立即发生而不是在10(= 0 + 1 + 2 + 3 + 4)秒之后,我认为是.是这样的,还是我错了?
import time
def some(sec):
time.sleep(sec)
return True
print(any(some(x) for x in range(5)))
Run Code Online (Sandbox Code Playgroud) 在Python世界中,似乎有两个术语是相等的:
两者之间有什么区别吗?
当使用 SQLAlchemy 从数据库中检索行时,您可以使用
query = "SELECT some_col FROM some_table"
row = session.execute(query).fetchone()
Run Code Online (Sandbox Code Playgroud)
但是,您也可以这样做:
query = "SELECT some_col FROM some_table LIMIT 1"
row = session.execute(query).fetchall()
Run Code Online (Sandbox Code Playgroud)
是否有任何理由选择其中一种而不是另一种,例如更好的性能?
有点蟒蛇新手,但我得到了以下的元组列表.我需要按值对其进行排序,如果值相同,则按字母顺序求解关系.这是一个示例:
#original
list_of_medals = [('Sweden', 24), ('Germany', 16), ('Russia', 10), ('Ireland', 10), ('Spain', 9), ('Albania', 8), ('Lithuania', 7), ('Iceland', 6), ('Malta', 5), ('Italy', 5), ('Serbia', 4), ('Estonia', 4), ('Turkey', 4), ('Moldova', 2), ('Azerbaijan', 2)]
# \____/ \_____/ \______/
#after sorting / \ / \ / \
sorted_medals = [('Sweden', 24), ('Germany', 16), ('Ireland', 10), ('Russia', 10), ('Spain', 9), ('Albania', 8), ('Lithuania', 7), ('Iceland', 6), ('Malta', 5), ('Italy', 5), ('Estonia', 4), ('Serbia', 4), ('Turkey', 4), ('Azerbaijan', 2), ('Moldova', 2)]
Run Code Online (Sandbox Code Playgroud)
是否可能使用该operator …
任务 你得到一个字符串。它由字母数字字符、空格和符号(+、-)组成。您的任务是找到包含两个或多个元音的原始字符串的所有子字符串。此外,这些子串必须位于辅音之间,并且应仅包含元音。
输入格式:包含 string 的单行输入。
输出格式:在单独的行上按出现顺序打印匹配的子字符串。如果未找到匹配项,则打印 -1。
样本输入:
rabcdeefgyYhFjkIoomnpOeorteeeeet示例输出:
ee
Ioo
Oeo
eeeee
上面的挑战来自https://www.hackerrank.com/challenges/re-findall-re-finditer
以下代码通过了所有测试用例:
import re
sol = re.findall(r"[^aiueo]([aiueoAIUEO]{2,})(?=[^aiueo])", input())
if sol:
for s in sol:
print(s)
else:
print(-1)
Run Code Online (Sandbox Code Playgroud)
以下没有。
import re
sol = re.findall(r"[^aiueo]([aiueoAIUEO]{2,})[^aiueo]", input())
if sol:
for s in sol:
print(s)
else:
print(-1)
Run Code Online (Sandbox Code Playgroud)
他们之间唯一的区别是正则表达式的最后一位。我不明白为什么第二个代码失败。我认为这?=是无用的,因为通过分组[aiueoAIUEO]{2,}我已经将它排除在捕获之外,但显然我错了,我不知道为什么。
有什么帮助吗?
我倾向于n-gram,并建立一个字典来保存n-gram值.我有这样的事情:
{
"it is" : 0.01,
"this is" : 0.005,
"hello i" : 0.2
"hello you" : 0.3
...
}
Run Code Online (Sandbox Code Playgroud)
我的字典有大约300万个密钥,需要0.0002(s)得到一个二元值.
有什么比dict我能用的更快吗?
给定以下函数:
def test(* p1=None, p2=None):
...
Run Code Online (Sandbox Code Playgroud)
调用方式如下:
test(p2="hello")
Run Code Online (Sandbox Code Playgroud)
我可以在运行时以编程方式获取参数及其值的列表/字典吗?
1:不想使用,**kwargs因为我想强制用户使用正确的参数名称(并计划进行类型注释)。
2:我查看了inspect获取默认值的模块,但似乎没有让我看到运行时值。
想要创建类似这样的代码:
request = {k: v for k,v in __some_magic_location__ if v is not None}
Run Code Online (Sandbox Code Playgroud) 这段代码:
from itertools import groupby, count
L = [38, 98, 110, 111, 112, 120, 121, 898]
groups = groupby(L, key=lambda item, c=count():item-next(c))
tmp = [list(g) for k, g in groups]
Run Code Online (Sandbox Code Playgroud)
采用[38, 98, 110, 111, 112, 120, 121, 898],按连续数字对其进行分组,并将它们与最终输出合并:
['38', '98', '110,112', '120,121', '898']
Run Code Online (Sandbox Code Playgroud)
如何对具有多列的列表进行同样的操作,就像下面的列表一样,您可以按名称及其第二列值的连续性对它们进行分组,然后合并。
换句话说,这个数据:
L= [
['Italy','1','3']
['Italy','2','1'],
['Spain','4','2'],
['Spain','5','8'],
['Italy','3','10'],
['Spain','6','4'],
['France','5','3'],
['Spain','20','2']]
Run Code Online (Sandbox Code Playgroud)
应给出以下输出:
[['Italy','1-2-3','3-1-10'],
['France','5','3'],
['Spain','4-5-6','2-8-4'],
['Spain','20','2']]
Run Code Online (Sandbox Code Playgroud)
more-itertools应该更适合这项任务吗?
在 Python 中使用 itertools/more-itertools 对多列列表的项目进行分组和组合
我有一个字典列表。我如何按值对该列表进行分组。
list = [{a:1},{b:2},{c:1},{d:3},{e:2}]
Run Code Online (Sandbox Code Playgroud)
现在我的结果应该如下所示
1:a,c
2:b,e
3:d
Run Code Online (Sandbox Code Playgroud)
我尝试使用groupbyfrom itertools。但我无法得到所需的结果。我正在使用 python 2.7。
你能帮我实现这个目标吗?
我正在查看print哪些状态的Python文档:
print(*objects,sep ='',end ='\n',file = sys.stdout,flush = False)
可以看出,如果file未指定要打印的内容,sys.stdout则使用默认值,这让我思考.
调用print肯定不会 sys在后台导入,那么它是如何工作的?
是sys.stdout莫名其妙地到达其他地方?
例:
我正在使用PyCharm,我想创建一个函数,可以将消息打印text到文件或标准输出.我开始写作:
def my_print(text, file=None):
print(text, file=file if file is not None else ?)
Run Code Online (Sandbox Code Playgroud)
那么接下来else呢?sys.stdout不起作用,显然,我对冗长不感兴趣:
def my_print(text, file=None):
if file is None:
print(text)
else:
print(text, file=file)
Run Code Online (Sandbox Code Playgroud) python ×9
python-3.x ×3
import ×2
dictionary ×1
list ×1
n-gram ×1
regex ×1
sorting ×1
sqlalchemy ×1
sys ×1
terminology ×1
tuples ×1
wording ×1