Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the equivalent of python zip() function in linux bash? [duplicate]

Tags:

bash

loops

I would like to iterate over pairs of elements in bash. In Python, this is straigthforward:

x_list = [1, 2, 3, 4]
y_list = ["a", "b", "c", "d"]

for x, y in zip(x_list , y_list):
    # here we have access to the values (i.e. the pairs are assigned to variables):
    result = do_something(x, y)
    print(result)

How can I replicate such a behavior in bash? I would need to store the values of the pairs into variables inside of the for loop, so that I can perform some operations. It should be something like:

for x, y in 1a 2b 3c 4d
  do 
     python script.py --x_value=${x} --y_value=${y}
  done

Note that the elements of the list could be also complicated stuff, such as: x_list = [1e-5, 1e-4, 1e-3] and y_list = [10, 20, 30]

like image 460
Gabriele Avatar asked Sep 02 '25 03:09

Gabriele


2 Answers

A possible translation of your code would be:

#!/bin/bash

x_list=(1 2 3 4)
y_list=(a b c d)

for i in "${!x_list[@]}"
do
    x=${x_list[i]}
    y=${y_list[i]}
    printf '%q %q\n' "$x" "$y"
done
1 a
2 b
3 c
4 d

Just be aware that it relies of the fact that the two newly created arrays will have identical indexes. Here's an illustration of the problem:

#!/bin/bash

x_list=(1 2 3 4)
y_list=(a b c d)

unset y_list[1]

for i in "${!x_list[@]}"
do
    x=${x_list[i]}
    y=${y_list[i]}
    printf '%q %q\n' "$x" "$y"
done
1 a
2 ''
3 c
4 d
like image 141
Fravadona Avatar answered Sep 04 '25 23:09

Fravadona


Here's an option using associative arrays

x_list=(1 2 3 "1e-5" 999)
y_list=("a" "b" "c" "d")

declare -A arr
max=$(( ${#x_list[@]} - 1 ))
# use the smallest size
echo "Sizes: x_list: ${#x_list[@]} y_list: ${#y_list[@]} max: $max"
if [ "${#x_list[@]}" -gt "${#y_list[@]}" ];then
    max=$(( ${#y_list[@]} - 1 ))
fi

for i in $(seq 0 $max);do
    arr[${x_list[$i]}]=${y_list[$i]}
    echo "${x_list[$i]} -> ${y_list[$i]}; ${arr[${x_list[$i]}]}"
done

Result

Sizes: x_list: 5 y_list: 4 max: 4
1 -> a; a
2 -> b; b
3 -> c; c
1e-5 -> d; d

Iterating over keys

for key in "${!arr[@]}"; do
    echo "key=$key, value=${arr[$key]}"
done

Result:

key=1, value=a
key=2, value=b
key=3, value=c
key=1e-5, value=d
like image 24
LMC Avatar answered Sep 04 '25 23:09

LMC