我的 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 中进行这种自定义排序吗?
当我处理数组时,我在 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)
应根据虚假声明提出例外情况
使用 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) 我有两个熊猫数据框:
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) 试图解决如何从不同线程将内容插入到 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) 我认为这将是一项相对容易完成的任务,但我在这里找不到不专注于根据列条件总结行的示例。我想要实现的是总结列重复,但保持行唯一。
这就是我的意思:
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) 成员函数的文档std::allocator<T>::allocate在 ( [allocator.members] ) 中说:
\n\n备注:数组的存储是通过调用 \xe2\x80\x8b
\n::\xe2\x80\x8boperator new([new.delete]) 获得的,但未指定该函数的调用时间和频率。该函数启动数组对象的生命周期,但不启动任何数组元素的生命周期。
我想知道为什么它说::operator new而不只是operator new?双冒号有什么区别吗?operator new如果省略了双冒号,这里可以调用哪个其他的?
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 预处理器,但似乎找不到在宏中存储状态的方法。
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) 相同的结果?
编辑:@ikegami 是第一个回应并指出我的错字的人。
\nraku正则表达式文档说:
\n\n\n要匹配任何单词边界,请使用 <|w> 或 <?wb>。这类似于其他语言中的 \\b。
\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\nRun Code Online (Sandbox Code Playgroud)\n<?wb>行为符合我的预期。在<|wb>做什么?
在 Perl 中:
\n"apa pz" =~ / \\b p. /xms; \nsay $&; # pz\nRun Code Online (Sandbox Code Playgroud)\n c++ ×2
r ×2
allocator ×1
c ×1
c++23 ×1
dataframe ×1
dplyr ×1
magrittr ×1
new-operator ×1
pandas ×1
pivot ×1
powershell ×1
preprocessor ×1
python ×1
raku ×1
reshape ×1
ruby ×1
rust ×1
std-ranges ×1