小编zep*_*iyr的帖子

Python Regex Sub - 在替换中使用匹配作为Dict键

我正在将程序从Perl翻译成Python(3.3).我对Python很新.在Perl中,我可以做狡猾的正则表达式替换,例如:

$string =~ s/<(\w+)>/$params->{$1}/g;
Run Code Online (Sandbox Code Playgroud)

这将搜索$string,并且对于<>中包含的每组单词字符,$params将使用正则表达式匹配作为哈希键从hashref 进行替换.

简洁地复制这种行为的最佳(Pythonic)方法是什么?我想出了这些方面的东西:

string = re.sub(r'<(\w+)>', (what here?), string)
Run Code Online (Sandbox Code Playgroud)

如果我可以传递将正则表达式匹配映射到dict的函数,那可能会很好.那可能吗?

谢谢您的帮助.

python regex perl python-3.x

6
推荐指数
1
解决办法
2854
查看次数

Correlated Subqueries with Django's ORM

Is there a way to model a correlated subquery using Django's ORM? Did I overlook this in the documentation somewhere?

I'm using Python 3.3, Django 1.7, and Django REST Framework 3.0.0. This is all against a legacy database - Django Models are in Managed = False mode.

In one of my ModelViewSets, I'm trying to set queryset. If I were writing SQL, what I need is:

select * from table
where dateField = (
    select max(dateField)
    from table …
Run Code Online (Sandbox Code Playgroud)

python django django-filter correlated-subquery django-rest-framework

5
推荐指数
0
解决办法
754
查看次数