我正在尝试实现一个函数来生成列表的powerset xs.
一般的想法是我们遍历元素xs并选择是否包括x.我面临的问题是withX最终等于[None](单个列表None)因为(我认为)s.add(x)返回None.
这不是一个家庭作业,它是一个破解编码面试的练习.
def powerSetBF(xs):
powerSet = []
powerSet.append(set([]))
for x in xs:
powerSetCopy = powerSet[:]
withX = [s.add(x) for s in powerSetCopy] # add x to the list of sets
powerSet = powerSet.extend(withX) # append those entries
return powerSet
Run Code Online (Sandbox Code Playgroud) 假设我生成了一个frozenset
A = frozenset(frozenset([element]) for element in [1,2,3])
Run Code Online (Sandbox Code Playgroud)
我有空集
E = frozenset(frozenset())
Run Code Online (Sandbox Code Playgroud)
现在我想要两个集合的并集:
U = A | E
Run Code Online (Sandbox Code Playgroud)
这给了我
frozenset({frozenset({2}), frozenset({3}), frozenset({1})})
Run Code Online (Sandbox Code Playgroud)
这假设,包含空的frozenset 的frozenset 本身是空的。但是,我想拥有
frozenset({frozenset({}), frozenset({2}), frozenset({3}), frozenset({1})})
Run Code Online (Sandbox Code Playgroud)
所以,我想将空集显式添加到集合中。例如,在我看来,在构建幂集时这是必要的吗?
那么:一个只包含空集本身的集合族是空的吗?在 Python 中,有没有一种方法可以使用变量类型set和frozenset?
给定一个整数 n (n >= 1) 和一个数字 k,返回将 n 写为 k 次方不同整数的所有可能方法。例如,如果 n = 100 且 k = 2:
100 = 1**2 + 3**2 + 4**2 + 5**2 + 7**2
= 6**2 + 8**2
= 10**2
Run Code Online (Sandbox Code Playgroud)
或者如果 k = 3:
100 = 1**3 + 2**3 + 3**3 + 4**3
Run Code Online (Sandbox Code Playgroud)
因此program(100,2)返回类似[(2, [1, 3, 4, 5, 7]), (2, [6, 8]), (2, [10])], and 的内容program(100,3) [(3, [1, 2, 3, 4])]。
只要输入 n 很小,或者 k 是“大”(>=3),一切都会正常工作。我的方法是首先获取所有整数的列表,其 k 次幂 <= …
我想找到排序字符串的所有子集,忽略顺序以及哪些字符彼此相邻.我认为解释这个的最佳方式是一个例子.结果也应该从最长到最短.
这些是结果bell.
bell
bel
bll
ell
be
bl
el
ll
b
e
l
Run Code Online (Sandbox Code Playgroud)
我已经想到了如何做到这一点,但没有任何长度的输入.谢谢!
我正在尝试获取数据帧八列之间每个可能组合的计数(所有行值为1)。基本上,我需要了解不同重叠存在多少次。
我试图用它itertools.product来获得所有组合,但似乎不起作用。
import pandas as pd
import numpy as np
import itertools
df = pd.read_excel('filename.xlsx')
df.head(15)
a b c d e f g h
0 1 0 0 0 0 1 0 0
1 1 0 0 0 0 0 0 0
2 1 0 1 1 1 1 1 1
3 1 0 1 1 0 1 1 1
4 1 0 0 0 0 0 0 0
5 0 1 0 0 1 1 1 1 …Run Code Online (Sandbox Code Playgroud) 我正在尝试生成电源组并添加powerset的元素.这就是我所做的.
例:
Given N=3,
S={1,2,3}
P(S) = {{1}, {2}, {3}, {1,2}, {1,3}, {2,3}, {1,2,3}}
answer = (1)+(2)+(3)+(1+2)+(1+3)+(2+3)+(1+2+3)
= 24
Run Code Online (Sandbox Code Playgroud)
样本I/O:
输入:1 3
产量:24
我的代码:
from itertools import combinations, chain
j = int(input())
for z in range(j):
x = int(input())
a_set = set()
for m in range(x):
a_set.add(m + 1)
lst = []
for q in chain.from_iterable(combinations(a_set, r) for r in range(len(a_set) + 1)):
lst.append(sum(q))
print(sum(lst))
Run Code Online (Sandbox Code Playgroud)
我得到了正确的输出,但计算更大的数字需要更多的时间.
Input
First line has T, the total number of test cases.
The next T …Run Code Online (Sandbox Code Playgroud) 所以我想创建一个带正整数n并返回一堆的函数n-tuples,填充所有可能的组合True/False (1/0),例如:
f(1) = (0,),(1,)
f(2) = (0, 0), (0, 1), (1, 0), (1, 1)
Run Code Online (Sandbox Code Playgroud)
我的代码是:
def fill(n: int) -> Tuple[Tuple[int]]:
if n == 1:
return (0,),(1,)
return tuple((i + j) for i in fill(n-1) for j in fill(1))
Run Code Online (Sandbox Code Playgroud)
我听说python不太适合递归,一般觉得这不是有效的解决方案.
看起来像使用给定数量范围的powerset(powerset的配方来自itertools模块)然后使用某种指示器功能就可以做到这一点.
from itertools import chain, combinations
def range_powerset(n: int):
s = list(range(n))
return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))
def indicator(A: Iterable, B: Iterable):
return tuple(i in A for i …Run Code Online (Sandbox Code Playgroud) 我在python中有一个字符串,我需要找到所有可能的方法,可以选择该字符串的任何子字符串(包括它自己).子串(为了我的目的)不必在原始字符串中是连续的 - 它可能有间隙.
例如:"frogman"是"froghuman'此定义下的众多子串之一.
例如,will函数:如果我的字符串是"abcd",输出应该是:
["a","b","c","d","ab","ac","ad","bc","bd","cd","abc","abd","acd","bcd","abcd"]
Run Code Online (Sandbox Code Playgroud) python ×8
python-3.x ×4
substring ×2
combinations ×1
frozenset ×1
math ×1
numpy ×1
optimization ×1
pandas ×1
powerset ×1
set ×1
string ×1
subset ×1