您现在的位置是:首页 > 电脑 > 

用xh列出理解(List comprehension with xh)

2025-07-27 03:12:36
用xh列出理解(List comprehension with xh) 我还是新手,但是可以使用list-comprehension语法在xh执行多个命令吗? 我希望以下内容创建五个文件file00到file04 ,但它错误: $ [@(['touch', 'file%02d' % i]) for i in range(5)] ...
用xh列出理解(List comprehension with xh)

我还是新手,但是可以使用list-comprehension语法在xh执行多个命令吗?

我希望以下内容创建五个文件file00到file04 ,但它错误:

$ [@(['touch', 'file%02d' % i]) for i in range(5)] ............................ xh: For full traceback set: $XOSH_SHOW_TRACEBACK = True File "<string>", line one SyntaxError: <xh-code>:1:1: ('code: @(',) [@(['touch', 'file%02d' % i]) for i in range(5)] ^

我希望这可行,因为以下工作正常:

$ [i for i in range(5)] [0, 1, 2, , 4] $ @(['touch', 'file%02d' % ]) $ ls file0

I am still new to this, but is it possible to execute multiple commands in xh using a list-comprehension syntax?

I would expect the following to create five files file00 to file04, but it errors instead:

$ [@(['touch', 'file%02d' % i]) for i in range(5)] ............................ xh: For full traceback set: $XOSH_SHOW_TRACEBACK = True File "<string>", line one SyntaxError: <xh-code>:1:1: ('code: @(',) [@(['touch', 'file%02d' % i]) for i in range(5)] ^

I would expect this to work, because the following works fine:

$ [i for i in range(5)] [0, 1, 2, , 4] $ @(['touch', 'file%02d' % ]) $ ls file0

最满意答案

最接近原始代码的方法是使用子进程:

[$[touch @('file%02d' % i)] for i in range(5)]

为了解释嵌套$[ .. @( :

顶级命令是列表理解,所以我们从Python模式开始; 我们想要执行一个bash命令( touch ),所以我们需要用$[ (或$(捕获输出)进入子进程模式; 但是该命令的参数需要使用Python进行字符串插值,因此再次使用@( 。

The way closest to your original code is to use a subprocess:

[$[touch @('file%02d' % i)] for i in range(5)]

To explain the need for nesting $[ .. @(:

The top-level command is a list-comprehension, so we start in Python-mode; We want to execute a bash command (touch) so we need to enter subprocess-mode with $[ (or $( to capture output); But the argument to that command requires string interpolation with Python, hence Python-mode again with @(.

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/diannao/77676.html

相关标签:无
上传时间: 2023-04-27 11:42:59
留言与评论(共有 20 条评论)
本站网友 纽约泛欧证券交易所
27分钟前 发表
$ [@(['touch'
本站网友 豪华游轮旅游
8分钟前 发表
但是可以使用list-comprehension语法在xh执行多个命令吗? 我希望以下内容创建五个文件file00到file04
本站网友 冷拔无缝管
22分钟前 发表
$ [@(['touch'
本站网友 日k线图
30分钟前 发表
) [@(['touch'
本站网友 奥硝唑片的功效
29分钟前 发表
1
本站网友 科学生男生女预测表
13分钟前 发表
本站网友 百香果加蜂蜜的功效与作用
8分钟前 发表
'file%02d' % i]) for i in range(5)] ^ I would expect this to work
本站网友 李炜博客
26分钟前 发表
) [@(['touch'
本站网友 展台效果图
23分钟前 发表
) [@(['touch'
本站网友 500强名单
26分钟前 发表
'file%02d' % i]) for i in range(5)] ^ I would expect this to work
本站网友 安胎丸
7分钟前 发表
[$[touch @('file%02d' % i)] for i in range(5)] To explain the need for nesting $[ .. @(
本站网友 党参炖鸡
2分钟前 发表
but is it possible to execute multiple commands in xh using a list-comprehension syntax? I would expect the following to create five files file00 to file04
本站网友 艾灸注意事项
13分钟前 发表
$XOSH_SHOW_TRACEBACK = True File "<string>"
本站网友 金港汽车公园
28分钟前 发表
'file%02d' % ]) $ ls file0 I am still new to this
本站网友 封闭式提问
24分钟前 发表
'file%02d' % ]) $ ls file0 最满意答案 最接近原始代码的方法是使用子进程: [$[touch @('file%02d' % i)] for i in range(5)] 为了解释嵌套$[ .. @( : 顶级命令是列表理解
本站网友 金华悦府江南
12分钟前 发表
'file%02d' % i]) for i in range(5)] ^ 我希望这可行
本站网友 新生儿肺炎治疗
5分钟前 发表
hence Python-mode again with @(.
本站网友 robomasters
26分钟前 发表
所以我们需要用$[ (或$(捕获输出)进入子进程模式; 但是该命令的参数需要使用Python进行字符串插值
本站网友 中麦通信
30分钟前 发表
'file%02d' % i]) for i in range(5)] ............................ xh