本着短代码的精神......我有一个数字或空字符串的列表,我希望map(int, mylist)不失败。
mylist = "123//456".split("/") #['123', '', '456']
myints = <something to quickly convert the list>
Run Code Online (Sandbox Code Playgroud)
编辑 我需要保留相同数量的令牌。IE myints = [123, 0, 456]。
我正在考虑类似的事情map(lambda x: int(x) or 0, mylist),但这当然仍然会引发ValueError. 有任何想法吗?
另一个编辑
一些结果(时间for xrange(1000000))...
l = map(lambda x: x.isdigit() and int(x) or 0, mylist)l = [x.isdigit() and int(x) or 0 for x in mylist]l = map(lambda x: int(x) if x.isdigit() else 0, mylist)l = …只是为了看看CUDA生成了什么样的代码我喜欢除了目标文件之外还要编译成ptx.由于我的一些循环展开可能需要相当长一段时间,我想能够编译*.cu→ *.ptx→*.o而不是浪费时间既*.cu→*.ptx和*.cu→*.o,这我目前做的事情.
只需添加-ptx到该nvcc *.cu行即可获得所需的ptx输出.
使用ptxas -c编译*.ptx到*.o作品,但会导致我的可执行文件链接错误:Relocations in generic ELF (EM: 190).
尝试编译*.ptxwith 静默nvcc无效,不输出任何内容.
我需要传递一些选项ptxas吗?我应该如何使用单独的编译通过ptx正确编译?或者,我可以告诉nvcc保持ptx吗?
我有一个 bash 脚本,它从命令管道中生成一些文本。基于命令行选项,我想对输出进行一些验证。对于一个人为的例子......
CHECK_OUTPUT=$1
...
check_output()
{
if [[ "$CHECK_OUTPUT" != "--check" ]]; then
# Don't check the output. Passthrough and return.
cat
return 0
fi
# Check each line exists in the fs root
while read line; do
if [[ ! -e "/$line" ]]; then
echo "Error: /$line does not exist"
return 1
fi
echo "$line"
done
return 0
}
ls /usr | grep '^b' | check_output
Run Code Online (Sandbox Code Playgroud)
[编辑] 更好的例子:https : //stackoverflow.com/a/52539364/1888983
这真的很有用,特别是如果我有多个可以成为直通的函数。是的,我可以移动 CHECK_OUTPUT 条件并创建一个带有或不带有 check_output 的管道,但我需要为每个组合编写行以获得更多功能。如果有更好的方法来动态构建管道,我想知道。
问题是“猫的无用使用”。可以避免这种情况并使其check_output …
例如:
#include <stdlib.h>
#define A 20
#define B 22
#define C (A+B)
int main()
{
srand(time(0));
int i = (rand()&1) + C;
return i;
}
Run Code Online (Sandbox Code Playgroud)
在gdb中,
(gdb) print C
No symbol "C" in current context.
Run Code Online (Sandbox Code Playgroud)
我怎么知道C是什么?可以gdb告诉我吗?(我添加的rand()所以我们不能轻易推断出它是什么)
预处理器会将 C 替换为(20+22). 这个值可以在 debuginfo 中以某种方式打印吗?
在宏可能非常复杂的真实示例中,我不想浪费时间做预处理器的工作。
我有一个在后台运行的打印一些文本行的进程。如果我启动 ssh,换行符将无法正常工作。
user@localhost:~$ { sleep 5; echo -e "1\n2\n3" ; } &
[1] 26215
user@localhost:~$ 1
2
3
user@localhost:~$ { sleep 5; echo -e "1\n2\n3" ; } &
user@localhost:~$ ssh localhost # quickly
user@localhost:~$ 1
2
3
Run Code Online (Sandbox Code Playgroud)
我希望无论 ssh 是否正在运行,2 和 3 都会从换行符的开头开始。有人可以解释这里发生了什么吗?有没有办法解决问题,使换行符仍然有效?
奇怪的是,如果我添加一些\r换行符,换行符似乎又可以工作了,尽管我\r也需要用 a 替换默认的 echo 换行符。
user@localhost:~$ { sleep 5; echo -en "1\r\n2\r\n3\r\n" ; } &
[1] 7066
user@localhost:~$ 1
2
3
Run Code Online (Sandbox Code Playgroud)