- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设您有一个包含 y 个位置的列表(对于这个问题,为 0)。如果 y = 10:
[0,0,0,0,0,0,0,0,0,0]
您想要将相邻位置填充到给定值 x 并将其附加到空列表中。如果 x = 4:
[[1,1,1,1,0,0,0,0,0,0], [0,1,1,1,1,0,0,0,0,0], [0,0,1,1,1,1,0,0,0,0], ... , [0,0,0,0,0,0,1,1,1,1]]
我通过这个函数实现了这一点:
def permutations(number=4, limit=10):
perms = []
if type(number) == int:
a = -1
b = a + number
while b < limit:
a+=1
b = a + number
start = [0 for x in range(limit)]
for i in range(a, b):
start[i] = 1
perms.append(start)
这很好,但如果我想做同样的事情,但传递一个元组而不是整数,我希望输出为:
如果数字 = (4,3):
[[1,1,1,1,0,1,1,1,0,0], [1,1,1,1,0,0,1,1,1,0], [1,1,1,1,0,0,0,1,1,1],
[0,1,1,1,1,0,1,1,1,0], [0,1,1,1,1,0,0,1,1,1],
[0,0,1,1,1,1,0,1,1,1]]
两个 1 分组之间的 0 是必需的,元组的第一个值对应于第一分组中 1 的数量,元组的第二个值对应于第二分组中 1 的数量。理想情况下,此函数适用于具有 2 个以上值的元组。
这个想法有点难以理解,所以如果您需要任何说明,请告诉我。
感谢您的帮助!
最佳答案
我能想到的最简单的方法是生成 1 和 0 的所有可能组合,并过滤掉所有不具有正确分组长度的组合。
import itertools
def permutations(tup, limit=10):
for candidate in itertools.product([0,1], repeat=limit):
segment_lengths = [len(list(b)) for a,b in itertools.groupby(candidate) if a == 1]
if tup == tuple(segment_lengths):
yield candidate
for seq in permutations((4, 3), 10):
print seq
结果:
(0, 0, 1, 1, 1, 1, 0, 1, 1, 1)
(0, 1, 1, 1, 1, 0, 0, 1, 1, 1)
(0, 1, 1, 1, 1, 0, 1, 1, 1, 0)
(1, 1, 1, 1, 0, 0, 0, 1, 1, 1)
(1, 1, 1, 1, 0, 0, 1, 1, 1, 0)
(1, 1, 1, 1, 0, 1, 1, 1, 0, 0)
请注意,对于较大的 limit
值来说,这非常慢 - 它必须评估 2^limit 候选序列。 limit = 10 还不错;只需要评估 1024 名候选人。但它很快就会增长到数百万甚至更多,并达到更大的限制。
编辑:受到 user2097159 的精彩评论的启发,这里有一个具有更好运行时间的方法。
import itertools
"""Finds all non-negative integer sequences whose sum equals `total`, and who have `size` elements."""
def possible_sums(total, size):
if total == 0:
yield [0]*size
return
if size == 1:
yield [total]
return
for i in range(total+1):
left = [i]
for right in possible_sums(total-i, size-1):
yield left + right
"""
combines two lists a and b in order like:
[a[0], b[0], a[1], b[1]...]
"""
def interleave(a,b):
result = []
for pair in itertools.izip_longest(a,b):
for item in pair:
if item is not None:
result.append(item)
return result
"""flattens a list of lists into a one dimensional list"""
def flatten(seq):
return [x for item in seq for x in item]
def permutations(tup, limit):
one_segments = [[1]*size for size in tup]
for i in range(len(tup)-1):
one_segments[i].append(0)
remaining_zeroes = limit - sum(tup) - len(tup) + 1
assert remaining_zeroes >= 0, "not enough room to separate ranges!"
for gap_sizes in possible_sums(remaining_zeroes, len(tup)+1):
zero_segments = [[0]*size for size in gap_sizes]
yield flatten(interleave(zero_segments, one_segments))
for seq in permutations((4, 3), 10):
print seq
关于python - 分多个步骤填写列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27534507/
在 JConsole 的 MBeans 选项卡中查看我的应用程序的 MBean 时,有“属性”和“操作”的子菜单。如果将这些树结构展开到最大程度,然后单击其中一个操作,则右侧面板会显示三个部分:操作调
我有一个包含稀疏数据的人性化工作表: PART | FRUIT --------------- Alpha | | Apples | Pears Beta |
我有一个包含每小时数据的数据框: area date hour output H1 2018-07-01 07:00:00 150 H1
假设我有这样的 html 表: 16 3 2 13 5 10
我已经将一些原始数据导入到 R 中,如下所示: 表 1: ID Year Value 01 1999 25 01 2000 12 01 2002
我已经将一些原始数据导入到 R 中,如下所示: 表 1: ID Year Value 01 1999 25 01 2000 12 01 2002
我在以下问题中遇到了同样的问题: Forward Fill New Row to Account for Missing Dates 不同之处在于,我需要计算两个不同日期之间的小时数差异,例如 201
我想填写 pandas 数据框中缺失的值。最佳情况下,我希望分钟列的范围为每小时 0-60。不幸的是,数据生成过程没有记录任何 sub_count = 0 的行。有办法做到这一点吗?我的数据涵盖日期
基本对象问题我似乎无法全神贯注。我确定我想多了。填写 addFullName 函数的代码。该函数应该: Take one input parameter, a person object. Add a
是否可以在没有用户交互的情况下调用 html 表单提交?我知道可以通过 HttpClient 发出发布请求,但这并不能解决我的问题。 我需要以编程方式在网页上填写一些表单输入字段,然后“单击”提交按钮
Python 的新手,似乎无法找到我正在寻找的确切答案我相信有更简单的方法来填写此信息 我有 df1 和 df2 df1: FirstName LastName PhNo uniqueid df
您好,我有一个需要填写的 PDF 表单。该应用程序向用户(表单)提出问题,提交时应将答案填写到 PDF 空白处以供打印。 我熟悉 JS 和 Node(有一段时间没用过)。不使用 PHP。 我会在我常去
假设我有一个如下所示的数据框: ID DATE VALUE 1 31-01-2006 5 1 28-02-2006 5 1 31
我想做的是,在填写四个字符时指向下一个选项卡。每个字段应有 4 个字符,完成后应移至下一个输入框。 $(".inputs").keyup(function () { if (this
我有 3 个 div,每个都有几个输入字段和下一步按钮。我想编写一个 jQuery 片段,当单击下一个按钮时,它会检查以确保与按钮位于同一 div 内的所有输入字段都不为空。 我已经尝试了以下但没有成
我正在做一个刽子手项目。我已经让大部分代码正常工作了。 我无法工作的部分是“ secret 单词”有多个相同的字母。例如“hello”有 2 个“l”。 这是代码部分的代码,如果猜测正确,它将“---
拥有抽象对象的集合:Set foes; 我想要一个这样的方法: List getFoesByType(TypeEnum type); 我已经尝试过: List result = new ArrayLi
我正在尝试使用 scrapy 填写 POST 表单,以尝试预订火车票。 我以为 FormRequest 类可以做这件事,但我无法处理 javascript 表单。 Scrapy 爬虫什么都不返回。 我
我使用以下代码生成带有渐变的图像。我逐个元素访问数组。有更好的方法吗?谢谢。 import cv2 import numpy as np x = np.ndarray((256,256,3), dty
我有一个数据对应于数据库列表和差异行,以及它们的使用日期。 DB Dates USAGE ABC 03-06-2018 IN USE
我是一名优秀的程序员,十分优秀!