我已经生成了一个组合列表,使用itertools并得到如下所示的结果:
nums = [-5,5,4,-3,0,0,4,-2]
x = [x for x in set(itertools.combinations(nums, 4)) if sum(x)==target]
>>> x = [(-5, 5, 0, 4), (-5, 5, 4, 0), (5, 4, -3, -2), (5, -3, 4, -2)]
Run Code Online (Sandbox Code Playgroud)
删除无序重复项(例如x[0]和x[1]是重复项)的时间复杂度最高的有效方法是什么。有没有内置的东西来处理这个问题?
我的一般方法是在一个元素中创建一个所有元素的计数器,然后与下一个元素进行比较。这会是最好的方法吗?
感谢您的任何指导。
我刚刚开始使用 numpy..
得到下面的 np 表,想计算每个单元格占列总数的百分比。
data = np.array([[7,16,17], [12,11,3]])
headers = ["Grundskola", "Gymn", "Akademisk"]
# tabulate data
table = tabulate(data, headers, tablefmt="github")
# output
print(table)
|   Grundskola |   Gymn |   Akademisk |
|--------------|--------|-------------|
|            7 |     16 |          17 |
|           12 |     11 |           3 |
Run Code Online (Sandbox Code Playgroud)
到:
|   Grundskola |   Gymn |   Akademisk |
|--------------|--------|-------------|
|           39%|    59% |         85% |
|           61%|    41% |         15% |
Run Code Online (Sandbox Code Playgroud)
我知道 np.sum(data2, axis=0/1) 会给我总数,但我如何使用它来计算数组。
数组的大小可以不同...
想根据计数、A 和 B 的值计算 C
示例 df:
| 数数 | 一种 | 乙 | C | 
|---|---|---|---|
| 是的 | 23 | 2 | 南 | 
| 南 | 23 | 1 | 南 | 
| 是的 | 41 | 6 | 南 | 
我想要的结果
| 数数 | 一种 | 乙 | C | 
|---|---|---|---|
| 是的 | 23 | 2 | 46 | 
| 南 | 23 | 1 | 0 | 
| 是的 | 41 | 6 | 246 | 
仅当计数值 = yes 时才计算 C = A*B 否则 C 值 =0 即应跳过计数的 nan 值
任何帮助都是可观的
我正在尝试这个
for ind, row in df.iterrows():
    if df['count'] == 'yes':
        df.loc[ ind, 'C'] =row['A'] *row['B']
    else:
        df.loc[ ind, 'C'] =0
Run Code Online (Sandbox Code Playgroud)
但它给出了错误: ValueError: 系列的真值不明确。使用 a.empty、a.bool()、a.item()、a.any() 或 …
我有一个数组,想要找到两个数字之间的平均值,并在两个数字之间添加一个附加元素。例如,如果我从
x = np.array([1, 3, 5, 7, 9])
Run Code Online (Sandbox Code Playgroud)
我想结束
[1, 2, 3, 4, 5, 6, 7, 8, 9]
Run Code Online (Sandbox Code Playgroud)
我该怎么做呢?
假设我有以下课程
class Example {
public:
    using value_type = std::tuple<
        uint8_t,
        uint8_t,
        uint16_t
    >;
private:
    value_type _value;
};
Run Code Online (Sandbox Code Playgroud)
现在,我希望能够基于此类型创建另一个类,将每个类类型包装在另一种类型中。基于将每种类型包装在模板化类中的可变参数模板中,我知道我可以通过以下方式实现一半的目标:
template <typename T>
class Wrapper;
template <typename ... ARGS>
class ExampleWrapper {
private:
    std::tuple<Wrapper<ARGS>...> _args;
};
Run Code Online (Sandbox Code Playgroud)
但是,我无法弄清楚的是,ARGS如果我所知道的就是ExampleT在哪里,如何获取TExample。我希望能够使用ExampleWrapper如下:
ExampleWrapper<Example> myWrapper;
Run Code Online (Sandbox Code Playgroud) 我根本无法很好地解释这个概念,但我正在尝试使用嵌套循环遍历列表,但我不知道如何使用相同的元素来避免它们。
list = [1, 2, 2, 4]
for i in list:
    for j in list:
        print(i, j) # But only if they are not the same element
Run Code Online (Sandbox Code Playgroud)
所以输出应该是:
1 2
1 2
1 4
2 1
2 2
2 4
2 1
2 2
2 4
4 1
4 2
4 2
Run Code Online (Sandbox Code Playgroud)
编辑,因为解决方案不适用于所有场景:
该if i != j解决方案仅在列表中的所有元素都不同时才有效,我显然选择了一个糟糕的示例,但我的意思是相同的元素而不是相同的数字;我改变了例子
我有corpus_text文本字符串,然后我将其转换为带有单词分割的列表
我需要计算所有单词,但我的算法只计算唯一的
corpus_test = 'cat dog tiger tiger tiger cat dog lion'
corpus_test = [[word.lower() for word in corpus_test.split()]]
word_counts = defaultdict(int)
for rowt in corpus_test:
    for wordt in rowt:
        word_counts[wordt] += 1
        v_count = len(word_counts.keys())
        words_list = list(word_counts.keys())
        word_index = dict((word, i) for i, word in enumerate(words_list))
        index_word = dict((i, word) for i, word in enumerate(words_list))
Run Code Online (Sandbox Code Playgroud)
我想向你展示这个算法的输出
v_count
#4
words_list
#['cat', 'dog', 'tiger', 'lion']
word_counts
#defaultdict(int, {'cat': 2, 'dog': 2, 'tiger': 3, 'lion': 1})
word_index
#{'cat': 0, …Run Code Online (Sandbox Code Playgroud) 我想在数据帧上滚动应用,但是我的自定义函数有问题,我想有一个额外的输入:
df_test = pd.DataFrame(columns=['amount'])
df_test['amount'] = [1, 2, 3, 4, 5]
mean = df_test['amount'].mean()
def rule(x,mean):
   x = x-mean
   return sum(x)
df_test['amount'].rolling(3).apply(rule,mean=mean)
Run Code Online (Sandbox Code Playgroud)
这返回
TypeError: apply() got an unexpected keyword argument 'mean'
Run Code Online (Sandbox Code Playgroud) 我试图对 DataFrame 的两列求和以创建第三列,其中第三列中的值等于其他列的正元素的总和。我尝试了下面的方法,只收到一列 NaN 值
df = pd.DataFrame(np.array([[-1, 2], [-2, 2], [1, -3], [1, -4], [ -2 , -2]]),
                   columns=['a', 'b'])
df['Sum of Positives'] = 0
df['Sum of Positives'] = df.loc[df.a > 0 ,'a'] +df.loc[df.b >0 , 'b']
Run Code Online (Sandbox Code Playgroud)
数据框:
我有以下数据帧:
| 用户身份 | 列_1 | 列_2 | 第 3 列 | 
|---|---|---|---|
| 一种 | 4.959 | 3.231 | 1.2356 | 
| 乙 | 0.632 | 0.963 | 2.4556 | 
| C | 3.234 | 7.445 | 5.3435 | 
| D | 1.454 | 0.343 | 2.2343 | 
我想对上一列的 argsort wrt 列进行排序:
| 用户身份 | 第一的 | 第二 | 第三 | 
|---|---|---|---|
| 一种 | 第 3 列 | 列_2 | 列_1 | 
| 乙 | 列_1 | 列_2 | 第 3 列 | 
| C | 列_1 | 第 3 列 | 列_2 | 
| D | 列_2 | 列_1 | 第 3 列 | 
python ×9
numpy ×4
pandas ×4
dataframe ×3
python-3.x ×3
c++ ×1
dictionary ×1
list ×1
percentage ×1
set ×1
stdtuple ×1
string ×1
templates ×1