我试图替换列表中的元素
# the raw data
square = ['(', ')', '.', '^', '-']
# the result I want
square = ['[', ']', '.', '^', '-']
Run Code Online (Sandbox Code Playgroud)
使用remove和的方法的多个步骤insert
In [21]: square.remove('(')
In [22]: square.remove(')')
In [23]: square.insert(0, '[')
In [24]: square.insert(1, ']')
In [25]: square
Out[25]: ['[', ']', '.', '^', '-']
Run Code Online (Sandbox Code Playgroud)
如何以一种直截了当的方式解决这样的问题?
在urls.pyDjango
#urls.py
url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic')
The second part of the expression, /(?P<topic_id>\d+)/, matches an integer between two forward slashes and stores the integer value in an argument called topic_id.
Run Code Online (Sandbox Code Playgroud)
我尝试用正则表达式来理解它
In [6]: re.findall(r'topics/(?P<topic_id>\d+)/$', "topics/1/")
Out[6]: ['1']
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试
In [7]: re.findall(r'topics/(?P<topic_id>\d+)/$', "topics/1/").topic_id
AttributeError: 'list' object has no attribute 'topic_id'
Run Code Online (Sandbox Code Playgroud)
似乎整数没有存储topic_id,如何理解呢?
我正在学习SQL"10分钟内的SQL",
引用使用通配符来检索所有记录,它声明:
通常,除非确实需要表中的每一列,否则最好不要使用*通配符.即使使用通配符可以节省显式列出所需列所需的时间和精力,但检索不必要的列通常会降低检索和应用程序的性能.
但是,检索所有记录所花费的时间少于检索多个字段所需的时间:
结果表明,通配符为0.02秒VS 0.1秒
我测试过几次,通配符比多个指定的列不断更快,即使每次消耗的时间都不同.
我正在阅读SICP 4.1.3 Evaluator Data Structures
(define (make-frame variables values)
(cons variables values))
(define (frame-variables frame) (car frame))
(define (frame-values frame) (cdr frame))
(define (add-binding-to-frame! var val frame)
(set-car! frame (cons var (car frame)))
(set-cdr! frame (cons val (cdr frame))))
Run Code Online (Sandbox Code Playgroud)
然而,set-car!据报道,球拍是无界的。
然后尝试了“GNU Guile 2.2.6”的实现,“GNU mit-scheme 10.1.10”,不幸的set-car!是,都没有绑定。
搜索原版全书:
find . -type f -iname "*.org" -exec grep --color -nH --null -e "set-car!" \{\} + |wc -l
27
Run Code Online (Sandbox Code Playgroud)
在第 3、4 和 5 章中出现了 27 次,
在第 3 章中我更改了 setcar!到 …
我正在努力深入挖掘并在学习Python方面更上一层楼.
在尝试学习collections模块的所有内容时,我在探索模块的每个角落时遇到了问题.在在线文档中help(collections),它引入了9种专用容器数据类型,见
8.3.集合 - 容器数据类型:
['Counter', 'OrderedDict', 'defaultdict', 'deque',
'namedtuple', 'ChainMap', 'UserDict', 'UserList', 'UserString']
Run Code Online (Sandbox Code Playgroud)
与该列表相比,文档中未指定其他26个,并且help()输出中的细节非常有限:
x = [ i for i in dir(collections) if not i.startswith('_')]
>>> list(enumerate(x, start=1))
[(1, 'AsyncGenerator'), (2, 'AsyncIterable'), (3, 'AsyncIterator'), (4, 'Awaitable'), (5, 'ByteString'), (6, 'Callable'),
(7, 'ChainMap'), (8, 'Collection'), (9, 'Container'), (10, 'Coroutine'), (11, 'Counter'), (12, 'Generator'), (13, 'Hashable'), (14, 'ItemsView'), (15, 'Iterable'), (16, 'Iterator'),
(17, 'KeysView'), (18, 'Mapping'), (19, 'MappingView'), (20, 'MutableMapping'), (21, 'MutableSequence'), (22, 'MutableSet'), …Run Code Online (Sandbox Code Playgroud) 我试图用default_if_none在username
<input type="text" class="form-control" id="username" value="{{ form.username.value|default_if_none: '' }}" name='username'>
Run Code Online (Sandbox Code Playgroud)
抛出错误
django.template.exceptions.TemplateSyntaxError: default_if_none requires 2 arguments, 1 provide
Run Code Online (Sandbox Code Playgroud)
我以前没有遇到过这样的错误。
该文档没有提供解决问题的提示。
我有这样一个模板来呈现数据库中的块
<div class="panel panel-default">
<div class="panel-heading">
<a style='font-size:18pt' href="article/list/{{ b.id }}">{{ b.name }}</a>
<span class='pull-right'>{{ b.admin }}</span>
</div>
<div class="panel-body">
{{ b.desc }}
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
数据模型:
class Block(models.Model):
STATUS = (
(1, 'normal'),
(0, 'deleted'),
)
name = models.CharField("block name", max_length=100)
desc = models.CharField("block description", max_length=100)
admin = models.CharField("block admin", max_length=100)
status = models.IntegerField(choices=STATUS)
class Meta:
ordering = ("id",)
def __str__(self):
return self.name
Run Code Online (Sandbox Code Playgroud)
我检索数据为
In [2]: from article.models import Block
In [3]: blocks = Block.objects.all()
In [4]: blocks
Out[4]: <QuerySet …Run Code Online (Sandbox Code Playgroud) 我正在关注"C Primer Plus"一书并遇到这样一段代码:
// designate.c -- use designated initializers
#include <stdio.h>
#define MONTHS 12
int main(void)
{
int days[MONTHS] = {31, 28, [4] = 31, 30, 31, [1] = 29};
int i;
for (i = 0; i < MONTHS; i++)
printf("%2d %d\n", i+1, days[i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它在编译时报告错误:
$ cc designate.c
designate.c:6:57: warning: initializer overrides prior initialization of this subobject [-Winitializer-overrides]
int days[MONTHS] = {31, 28, [4] = 31, 30, 31, [1] = 29};
^~
designate.c:6:29: note: previous initialization is …Run Code Online (Sandbox Code Playgroud) 我正在阅读APUE探索C和Unix的细节,并遇到 lseek
NAME
lseek - move the read/write file offset
SYNOPSIS
#include <unistd.h>
off_t lseek(int fildes, off_t offset, int whence);
Run Code Online (Sandbox Code Playgroud)
我是什么意思,是长度吗?