问题列表 - 第318124页

Powershell 自定义排序

我的 txt 文件中有以下内容,该文件将由 powershell 读取,并应根据自定义条件重新排列

文件:abc.txt

General
User1/Alert
User1/LogicApp
General/Abc
User2/Alert
User2/LogicApp
Run Code Online (Sandbox Code Playgroud)

上面应该按自定义条件排序,如果“General”存在,它应该位于列表顶部,如果字符串“LogicApp”存在,它应该位于 General 旁边。因此,输出应该类似于下面的内容

General
General/abc
User1/LogicApp
User2/LogicApp
User1/Alert
User2/Alert
Run Code Online (Sandbox Code Playgroud)

如果字符串“General”不存在,则字符串“LogicApp”应优先。我们可以在 Powershell 中进行这种自定义排序吗?

powershell powershell-4.0

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

Ruby 中如何解释嵌套数组的声明?

当我处理数组时,我在 Ruby 中的嵌套数组声明中错过了一个逗号,但语言以其他方式解释了它。如果有人熟悉这种行为,请考虑一下。

arr = [1,2[1,8]]
[1, 1] 
arr = [1,5[6,4]]
[1, 0] 
arr = [1,3[1,6]]
[1, 1] 
arr = [1,6[1,6]]
[1, 3] 
arr = [1,7[1,6]]
[1, 3] 
arr = [1,0[1,6]]
[1, 0] 
arr = [1,8[1,6]]
[1, 4]
Run Code Online (Sandbox Code Playgroud)

应根据虚假声明提出例外情况

ruby

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

使用多字符分隔符的 C++ 视图 join_with

使用 C++23 视图::join_with 时可以使用多字符串作为分隔符吗?

gcc 14 返回错误消息。

#include <format>
#include <stdio.h>
#include <string>
#include <algorithm>
#include <ranges>
using namespace std;

int main(){
    const string a[2]={"6", "7"};
    const string buffer1 = a | views::join_with('\n') | ranges::to<string>(); //OK
    printf("%s", buffer1.c_str());

    const string buffer2 = a | views::join_with(",\n") | ranges::to<string>(); //KO
    printf("%s", buffer2.c_str());
    return 0;
}
Run Code Online (Sandbox Code Playgroud)
prog.cc: In function 'int main()':
prog.cc:13:30: error: no match for 'operator|' (operand types are 'const std::string [2]' {aka 'const std::__cxx11::basic_string<char> [2]'} and 'std::ranges::views::__adaptor::_Partial<std::ranges::views::_JoinWith, const char*>')
   13 | …
Run Code Online (Sandbox Code Playgroud)

c++ std-ranges c++23

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

给定 pandas 列 DataFrame 中的值,选择与其他 DataFrame 中最接近的值上下的 N 行

我有两个熊猫数据框:

import pandas as pd

data1 = {
    'score': [1, 2],
    'seconds': [1140, 2100],
}

data2 = {
    'prize': [5.5, 14.5, 14.6, 21, 23, 24, 26, 38, 39, 40, 50],
    'seconds': [840, 1080, 1380, 1620, 1650, 1680, 1700, 1740, 2040, 2100, 2160],
}

df1 = pd.DataFrame.from_dict(data1)
df2 = pd.DataFrame.from_dict(data2)

Output: df1
   score  seconds
0      1     1140
1      2     2100

Output: df2
    prize  seconds
0     5.5      840
1    14.5     1080
2    14.6     1380
3    21.0     1620
4    23.0     1650
5    24.0 …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

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

rust:跨多个线程使用 hashmap arc 和 rwlock

试图解决如何从不同线程将内容插入到 hasmap 的问题,但仍然无法得到它。

为什么以下程序在每次执行时都会缺少一些键?我目前的理解是.write等待获取锁并.join等待所有线程完成?

因此,我希望最终所有线程都将它们的值插入到哈希图中,但显然我仍然缺少一些东西。

操场

use std::collections::HashMap;
use std::sync::Arc;
use std::sync::RwLock;
use std::thread;
use std::vec;

pub fn main() {
    let mut contacts: HashMap<String, String> = HashMap::new();
    contacts.insert("main-thread".to_owned(), "hello world".to_owned());

    let contacts = Arc::new(RwLock::new(contacts));
    let mut children = vec![];
    for _ in 0..10 {
        let lock_contacts = Arc::clone(&contacts);
        children.push(thread::spawn(move || {
            let num = thread::current().id();
            let num = format!("{num:?}");
            if let Ok(mut contacts) = lock_contacts.write() {
                contacts.insert(num.clone(), "hey".to_owned());
            }
        }));
    }
    let _ = children.into_iter().map(|c| c.join()); …
Run Code Online (Sandbox Code Playgroud)

rust

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

总结数据框中的重复列,保持行唯一

我认为这将是一项相对容易完成的任务,但我在这里找不到不专注于根据列条件总结行的示例。我想要实现的是总结列重复,但保持行唯一。

这就是我的意思:

                                 MKC100.1 MKC100.2 MKC100.3 MKC103.1 MKC103.2 MKC103.3 MKC104.2 MKC104.3
299fc0ac11fb4afd0da849a2c45583b3        0        0        0        0        0        0        0        1
9bc2bacdfadf4c1352ffbc991803287c     1183     1666     1318        0        0        0       10       20
38b782d9f01c69c3570fe0edd5864dc0      493      626      543       10        0        0        5        5
6d078397349f7d39c34d237a6ef4cb75    43735    51511    46876        0        0        0        1        0
c22e752b441ee4190f27a3690c5d1206        0        0        0     2795     1128     1956        1        1
f6513affb198fb9845741b61ece8db4b       59       58       82        0        0        0        0        0



structure(list(MKC100.1 = c(0L, 1183L, 493L, 43735L, 0L, 59L), 
    MKC100.2 = c(0L, 1666L, 626L, 51511L, 0L, 58L), …
Run Code Online (Sandbox Code Playgroud)

pivot r reshape

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

为什么 std::allocator&lt;T&gt;::allocate 调用 ::operator new?

成员函数的文档std::allocator<T>::allocate在 ( [allocator.members] ) 中说:

\n
\n

备注:数组的存储是通过调用 \xe2\x80\x8b::\xe2\x80\x8boperator new ([new.delete]) 获得的,但未指定该函数的调用时间和频率。该函数启动数组对象的生命周期,但不启动任何数组元素的生命周期。

\n
\n

我想知道为什么它说::operator new而不只是operator new?双冒号有什么区别吗?operator new如果省略了双冒号,这里可以调用哪个其他的?

\n

c++ new-operator allocator language-lawyer

10
推荐指数
1
解决办法
475
查看次数

具有状态的 C 宏

C 宏是否可以具有任何状态?在本例中,为整数值。

我希望能够从静态分配的缓冲区中分配一些内存,因为我处于嵌入式环境中。假设我有一个像这样的结构和一个缓冲区:

double buffer[1000];

typedef struct {
    int rows;
    int cols;
    double* data;
} matrix;
Run Code Online (Sandbox Code Playgroud)

我想要一个可以生成矩阵并从相关缓冲区分配相关内存的宏。然而,我似乎想不出一种方法来保存内部计数器来确定我们目前应该在缓冲区中的位置。理想情况下,宏应该是这样的:

#define alloc_matrix(_rows, _cols) {.rows = _rows, .cols = _cols, .data = &buffer[PTR]}
Run Code Online (Sandbox Code Playgroud)

其中 PTR 是编译时间常数,每次使用 alloc_matrix 时都会不断变化(按行 * 列增加)。这将允许我在编译时使用 _Static_assert 之类的东西检查所有内容是否适合缓冲区,而不是在运行时检查所有内容。

这可以用 C 预处理器实现吗?

我尝试过使用 C 预处理器,但似乎找不到在宏中存储状态的方法。

c preprocessor

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

如何将which()函数与管道一起使用%&gt;%

test <- c(1,2,0,5,0)

which(test == 0)

test %>% which(.==0)
test %>% which(,==0)
test %>% which( ==0)
Run Code Online (Sandbox Code Playgroud)

我试图弄清楚如何将向量通过管道传输到which()函数中。使用which(test == 0)给了我想要的结果,但是在最后三行使用管道都会给我错误。如何使用管道获得与 which(test == 0) 相同的结果?

r dplyr magrittr

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

正则表达式单词边界: &lt;|wb&gt; v. &lt;?wb&gt;

编辑:@ikegami 是第一个回应并指出我的错字的人。

\n

raku正则表达式文档说:

\n
\n

要匹配任何单词边界,请使用 <|w> 或 <?wb>。这类似于其他语言中的 \\b。

\n
\n

这是我在 rakudo 中看到的:

\n
[309] > "apa pz" ~~ / <|wb> p. /\n\xef\xbd\xa2pa\xef\xbd\xa3\n\n[310] > "apa pz" ~~ / <?wb> p. /\n\xef\xbd\xa2pz\xef\xbd\xa3\n
Run Code Online (Sandbox Code Playgroud)\n

<?wb>行为符合我的预期。在<|wb>做什么?

\n

在 Perl 中:

\n
"apa pz" =~ / \\b p. /xms;  \nsay $&;   # pz\n
Run Code Online (Sandbox Code Playgroud)\n

raku

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