bash 中的 Python zip() 行为?

标签 python bash

bash 中是否有类似的 Python zip() 功能?具体来说,我在不使用 python 的情况下寻找 bash 中的等效功能:

$ echo "A" > test_a
$ echo "B" >> test_a
$ echo "1" > test_b
$ echo "2" >> test_b
$ python -c "print '\n'.join([' '.join([a.strip(),b.strip()]) for a,b in zip(open('test_a'),open('test_b'))])"
A 1
B 2

最佳答案

纯 bash:

liori@marvin:~$ zip34() { while read word3 <&3; do read word4 <&4 ; echo $word3 $word4 ; done }
liori@marvin:~$ zip34 3<a 4<b
alpha one
beta two
gamma three
delta four
epsilon five
liori@marvin:~$

(旧答案)查看 join

liori:~% cat a
alpha
beta
gamma
delta
epsilon
liori:~% cat b
one
two
three
four
five
liori:~% join =(cat -n a) =(cat -n b)
1 alpha one
2 beta two
3 gamma three
4 delta four
5 epsilon five

(假设你有 =() operator like in zsh ,否则会更复杂)。

关于bash 中的 Python zip() 行为?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6145540/

相关文章:

python - 使用 Python 将最新版本的文件从网站下载到特定位置

python - 使用 Pycparser 解析变量依赖关系

python - 安装 distutils 包时如何使用 mingw32?

python - 具有特定数组条件的 while 循环

linux - 命令输出位置

bash - 无法在docker中访问Windows Linux容器的Elasticsearch

linux - Bash 查找和表达

python - 为什么在UDP中的UDP流接收器中视频损坏了?

bash - 在单引号字符串中的 bash 中转义单引号

linux - 根据文本文件中的部分名称将文件复制或移动到另一个目录