小编ric*_*ier的帖子

优雅查找列表中的子列表

给定一个包含噪声包围的已知模式的列表,是否有一种优雅的方式来获得与模式相同的所有项目.请参阅下面的原始代码.

list_with_noise = [7,2,1,2,3,4,2,1,2,3,4,9,9,1,2,3,4,7,4,3,1,2,3,5]
known_pattern = [1,2,3,4]
res = []


for i in list_with_noise:
    for j in known_pattern:
        if i == j:
            res.append(i)
            continue

print res
Run Code Online (Sandbox Code Playgroud)

我们会得到的 2, 1, 2, 3, 4, 2, 1, 2, 3, 4, 1, 2, 3, 4, 4, 3

奖励:如果不存在完整模式,则避免附加i(即,允许1,2,3,4但不允许1,2,3)

例子:

find_sublists_in_list([7,2,1,2,3,4,2,1,2,3,4,9,9,1,2,3,4,7,4,3,1,2,3,5],[1,2,3,4])

[1,2,3,4],[1,2,3,4],[1,2,3,4]


find_sublists_in_list([7,2,1,2,3,2,1,2,3,6,9,9,1,2,3,4,7,4,3,1,2,6],[1,2,3,4])

[1,2,3],[1,2,3],[1,2,3]
Run Code Online (Sandbox Code Playgroud)

列表包含命名元组.

python design-patterns list

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

Ansible从命令行获取用户名

在我的剧本中,我引用了很多用户名(仅限其"ubuntu").

是否有内置的方式来说"从命令行传递的值中获取它"?

我知道我能做到

ansible-playbook <task> -u <user> -K --extra-vars "user=<user>"
Run Code Online (Sandbox Code Playgroud)

然后我可以{{user}}在剧本中使用,但定义用户两次感觉很奇怪.

ansible ansible-playbook

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

关于VBA正则表达式的看法?

有没有办法在VBA正则表达式中做出消极和积极的观察?

如果字符串以"A"开头,我想不匹配,所以我当前在模式的开头做^ A,然后删除match(0)的第一个字符.显然不是最好的方法!

我正在使用regExp对象.

regex vba negative-lookbehind lookbehind

14
推荐指数
1
解决办法
8323
查看次数

我可以用dotall获得非贪婪的正则表达式吗?

我想匹配dotall和非贪心.这就是我所拥有的:

img(.*?)(onmouseover)+?(.*?)a
Run Code Online (Sandbox Code Playgroud)

但是,这不是非贪婪的.这个数据不符合我的预期:

<img src="icon_siteItem.gif" alt="siteItem" title="A version of this resource is available on siteItem" border="0"></a><br><br></td><td rowspan="4" width="20"></td></tr><tr><td>An activity in which students find other more specific adjectives to 
describe a range of nouns, followed by writing a postcard to describe a 
nice holiday without using the word 'nice'.</td></tr><tr><td>From the resource collection: <a href="http://www.siteItem.co.uk/index.asp?CurrMenu=searchresults&amp;tag=326" title="Resources to help work">Drafting </a></td></tr><tr><td><abbr style="border-bottom:0px" title="Key Stage 3">thing</abbr> | <abbr style="border-bottom:0px" title="Key Stage 4">hello</abbr> | <abbr style="border-bottom:0px" title="Resources">Skills</abbr></td></tr></tbody></table></div></div></td></tr><tr><td><div style="padding-left: 30px"><div><table style="" bgcolor="#DFE7EE" border="0" cellpadding="0" cellspacing="5" width="100%"><tbody><tr …
Run Code Online (Sandbox Code Playgroud)

python regex non-greedy

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

装饰器设置功能的属性

我希望只有登录用户具有所需的权限级别才能执行不同的功能.

为了让我的生活更复杂,我只想使用装饰器.下面我尝试permission在'装饰'功能上设置属性- 如下所示.

def permission(permission_required):
    def wrapper(func):
        def inner(*args, **kwargs):
            setattr(func, 'permission_required', permission_required)
            return func(*args, **kwargs)
        return inner
    return wrapper

@permission('user')
def do_x(arg1, arg2):

    ...

@permission('admin')
def do_y(arg1, arg2):
    ...
Run Code Online (Sandbox Code Playgroud)

但当我这样做时:

fn = do_x
if logged_in_user.access_level == fn.permission_required:
    ...
Run Code Online (Sandbox Code Playgroud)

我收到一个错误 'function' object has no attribute 'permission_required'

我错过了什么?

python attributes decorator function-attributes python-decorators

8
推荐指数
1
解决办法
7488
查看次数

拆分列表中的项目

我该如何转换以下列表

['1','2','A,B,C,D','7','8']
Run Code Online (Sandbox Code Playgroud)

成

['1','2','A','B','C','D','7','8']

以最pythonic的方式?

我有非常unpythonic代码创建嵌套列表,然后flatterens:

sum ( [ word.split(',') for word in words ], [] )
Run Code Online (Sandbox Code Playgroud)

python split list

7
推荐指数
1
解决办法
3299
查看次数

强制任何正在运行的进程崩溃

我想崩溃我选择的运行程序(例如,notepad ++,becrypt,word)用于软件测试.

我知道如何BSOD,我知道如何导致我编写的程序崩溃,我知道如何结束进程 - 但是如何崩溃现有进程我不知道!

任何帮助?

windows crash testing

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

Django Rest Framework 序列化权限

我想序列化对象级权限 - 并向浏览器发送一个简单的True或False- 因此我触发了“允许编辑”代码。

我这样做是为了让用户没有向他们展示他们无法使用的功能。

有没有内置的方法来做到这一点?文档没有提到这一点。

我在序列化程序上尝试了以下操作:

has_permission = serializers.SerializerMethodField('check_permission')

def check_permission(self, obj):
    return self.check_object_permissions(self.request, obj)
Run Code Online (Sandbox Code Playgroud)

但序列化程序没有方法check_object_permissions- 属于权限对象。

django permissions django-rest-framework

5
推荐指数
1
解决办法
2879
查看次数

在python中使用| =

|=今天我在代码库中遇到了运营商并且没有认出它,并且搜索google python "|="返回对我没什么用.

测试下面的操作结果会产生以下结果:

>>> from itertools import combinations
>>> def wtf_is_it(left, right):
>>>     orig_left = left
>>>     left |= right
>>>     return '%s |= %s == %s ' % (orig_left, right, left)
>>> results = [wtf_is_it(l, r) for l, r in combinations(xrange(5), 2)]
>>> print '\n'.join(results)
0 |= 1 == 1 
0 |= 2 == 2 
0 |= 3 == 3 
0 |= 4 == 4 
1 |= 0 == 1 
1 |= 2 == 3 
1 …
Run Code Online (Sandbox Code Playgroud)

python operators

4
推荐指数
1
解决办法
1461
查看次数

将32位浮点数转换为16位PCM范围

我有一些由JavaScript HTML5网络音频API生成的数据.它生成Float32Array,一个32位浮点数组,介于-1和1之间.我使用websocket将数据流式传输到我的服务器.

我需要将32位浮点数转换为-32768和+32767(16位有符号整数)之间的16位PCM范围.然后,这允许将数据用作wav文件.

我在转换时遇到了麻烦.我怀疑答案是使用struct模块,但我无法获得正确的格式.

python audio

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