小编ret*_*327的帖子

如何创建具有相同键的字典列表?

假设我有以下三个列表:

list_1 = [1, 2, 3, 4, 5]
list_2 = ['a', 'b', 'c', 'd', 'e']
list_3 = [1.1, 2.2, 3.3, 4.4]
Run Code Online (Sandbox Code Playgroud)

如何将这三个列表组合成这样:

[
    {'int': 1, 'str': 'a', 'float': 1.1}, 
    {'int': 2, 'str': 'b', 'float': 2.2}, 
    {'int': 3, 'str': 'c', 'float': 3.3}, 
    {'int': 4, 'str': 'd', 'float': 4.4}, 
    {'int': 5, 'str': 'e', 'float': 5.5}
]
Run Code Online (Sandbox Code Playgroud)

实现此目的的语法最干净的方法是什么?

谢谢你的帮助!!

python dictionary list python-3.x

4
推荐指数
2
解决办法
254
查看次数

如何将数组中的多个对象转换为新对象?

假设我有一个下面的数组:

[
  { type: 'senior', schoolName: 'school-A', country: 'America' },
  { type: 'senior', schoolName: 'school-B', country: 'England' },
  { type: 'junior', schoolName: 'school-C', country: 'German' },
  { type: 'junior', schoolName: 'school-D', country: 'Italy' }, 
]
Run Code Online (Sandbox Code Playgroud)

如何将上面的数组转换为对象,如下所示:

{
  senior: {
    America: 'school-A', 
    England: 'school-B'
  }, 
  junior: {
    German: 'school-C', 
    Italy: 'school-D'
  }
}
Run Code Online (Sandbox Code Playgroud)

完成此任务的语法上最简洁的方法是什么?

谢谢你的帮助!!

javascript arrays object node.js

0
推荐指数
1
解决办法
1121
查看次数

Docker bash:语法错误:意外的“(”(期望“then”)

我尝试在 Docker 中运行 bash 脚本,但不断收到此错误:

./scripts/proto-generator.sh:第 13 行:语法错误:意外的“(”(期望“then”)

这是我的 proto-generator.sh 文件:

function printGreen() {
    printf "\e[0;32m$1\e[0;m\n"
}

function printRed() {
    printf "\e[0;31m$1\e[0;m\n"
}

service=$1
outDir=./src/services/$service/models
protoDir=./protos/"${service}Service"/*.proto

if ! [[ "$service" =~ ^(file|user)$ ]]; then
    printRed "Incorrect service: $service"
    exit 1
fi

./node_modules/.bin/proto-loader-gen-types \
    --longs=String \
    --enums=String \
    --defaults \
    --oneofs \
    --grpcLib=@grpc/grpc-js \
    --outDir=$outDir \
    $protoDir

printGreen "GRPC codes generated: ${outDir}"

Run Code Online (Sandbox Code Playgroud)

如何修复语法错误?

谢谢你的帮助!!

bash docker dockerfile

0
推荐指数
1
解决办法
4175
查看次数