我的示例字典是:
sample_dict = {
'company': {
'employee': {
'name': [
{'explore': ["noname"],
'valid': ["john","tom"],
'boundary': ["aaaaaaaaaa"],
'negative': ["$"]}],
'age': [
{'explore': [200],
'valid': [20,30],
'boundary': [1,99],
'negative': [-1,100]}],
'others':{
'grade':[
{'explore': ["star"],
'valid': ["A","B"],
'boundary': ["C"],
'negative': ["AB"]}]}
}
}}
Run Code Online (Sandbox Code Playgroud)
它是一个“后续”问题 ->拆分 python 字典以生成所有值组合
我想得到一个隔离的组合列表,如下所示
有效组合:[仅从有效数据列表中生成]
COMPLETE OUTPUT for VALID类别 :
{'company': {'employee': {'age': 20}, 'name': 'john', 'others': {'grade': 'A'}}}
{'company': {'employee': {'age': 20}, 'name': 'john', 'others': {'grade': 'B'}}}
{'company': {'employee': {'age': 20}, 'name': 'tom', 'others': {'grade': 'A'}}}
{'company': …Run Code Online (Sandbox Code Playgroud) 假设我有一个这样的专栏:
col
---
A
B
C
Run Code Online (Sandbox Code Playgroud)
我需要所有可能的行组合,如下所示:
result
---
A
B
C
A,B
A,C
B,C
A,B,C
Run Code Online (Sandbox Code Playgroud)
获得结果的最佳解决方案是什么?
我发现的大多数答案都建议对每一行进行连接,但我需要一些东西来覆盖不同的行数
我需要编写一个函数来随机化我的字符串中的一些单词.例如:
[Hello|Hi] guys. This is my [code|string]
Run Code Online (Sandbox Code Playgroud)
该函数应返回:
Hello guys. This is my code
要么
Hi guys. This is my string
如何在Python中找到包含3个元素的列表的所有排列?
例如,输入
[1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
会回来的
[1, 2, 3]
[1, 2, 4]
[1, 3, 4]
[2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
谢谢!
想象一下我有一个字符串,abc。我想找到可以从该字符串中的字母创建的所有组合/排列。
例如:
abc
bac
acb
cba
...
Run Code Online (Sandbox Code Playgroud)
我怎样才能用 Perl 做到这一点?
from itertools import combinations
def n_length_combo(arr, n):
# using set to deal
# with duplicates
return list(combinations(arr, n))
# Driver Function
if __name__ == "__main__":
arr = '01'
n = 3
print (n_length_combo([x for x in arr], n) )
Run Code Online (Sandbox Code Playgroud)
想要 0 和 1 的 3 个组合。尝试了上面的示例,但它不起作用
我正在尝试创建一个函数isPerm(P),True如果它是一个排列,则返回,False否则.
到目前为止我有:
def isPerm(P):
if len(P) == list(range(P)):
return True
else:
return False
Run Code Online (Sandbox Code Playgroud)
任何帮助都会很棒.
尝试生成排列,可以与生成器一起使用或生成列表列表(但我可能需要大量内存?)在Internet和SO上查看,但找不到我为每个元素定义值的版本.
顺便说一下它会有多少种排列?
8个元素,每个值从1到15
这是我的代码,但也许有更好,更快的方法来生成它:
任何提示表示赞赏!
import time
from tqdm import tqdm
def permutations(iterable, r=None):
# permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
if r > n:
return
indices = range(n)
cycles = range(n, n-r, -1)
yield tuple(pool[i] for i in indices[:r])
while n:
for i in reversed(range(r)):
cycles[i] …Run Code Online (Sandbox Code Playgroud) 我试图计算所有可能的3个字母排列,使用26个字母(仅相当于26*25*24 = 15,600).字母的顺序很重要,我不想重复字母.(我希望按字典顺序生成排列,但这不是必需的)
到目前为止,我试图嵌套for循环,但我最终迭代了每一个可能的组合.所以有重复的字母,我不想要,for如果我想要超过3个字母,循环可能变得难以管理.
我可以通过字母翻转,直到我尚未使用的信,但它不是在字典顺序,这是很多比使用慢next_permutation(我不能使用这种std方法,因为我离开了计算所有的子集26个字母).
有没有更有效的方法来做到这一点?从低效率的角度来看,next_permutation即时迭代前6位数字.但是,使用这种方法获取所有三个字母的排列需要几秒钟,而且next_permutation我必须计算的2 ^ n个子集仍然很快变得低效.
这是我对嵌套for循环的看法:
char key[] = {'a','b','c','d','e','f','g','h','i','j','k',
'l','m','n','o','p','r','s','t','u','v','w','x','y','z'};
bool used[25];
ZeroMemory( used, sizeof(bool)*25 );
for( int i = 0; i < 25; i++ )
{
while( used[i] == true )
i++;
if( i >= 25 )
break;
used[i] = true;
for( int j = 0; j < 25; j++ )
{
while( used[j] == …Run Code Online (Sandbox Code Playgroud) 我正在寻找使用 C+++ 的 Permutation、Combination 和 PowerSet 的实现
permutation ×11
combinations ×5
python ×5
c# ×2
c++ ×2
.net ×1
c++11 ×1
dictionary ×1
list ×1
nested ×1
performance ×1
perl ×1
powerset ×1
random ×1
sql ×1
sql-server ×1
t-sql ×1