如何获得重复运动的各自名称和权重...?

时间:2019-02-07 05:05:38

标签: python python-3.x

我有3本字典。...

fi'
./problem6.bash: line 11:

在这里,“曲棍球”分为两组,并且权重不同...

对于仅具有各自权重的一组名称,还要进行一些权重计算...

我想要的是一项运动,应该提取各个权重,并对各个组名进行计算。

在这种情况下:



    #!/bin/bash
    parameters=("$@")
    s=0
    for numbers in "${parameters[@]}"
    do
    if [ "$numbers" -gt 10 ];
    then
    s=(($numbers + $s))

    fi

    done

    echo "Total sum: $s"

应该用于计算...

到目前为止,我已经做了很多...

dic1 = {
    'g1': ['akshay', 'swapnil', 'parth','juhi'], 
    'g2': ['megha', 'varun', 'tarun'], 
    'g3': ['gaurav', 'geeta']
}
dic2 = {
    'g1': ['hockey', 'basketball', 'football'], 
    'g2': ['cricket', 'chess', 'badminton', 'carrom'], 
    'g3': ['pool', 'hockey', 'tabletennis']
}
dic3 = {
    'g1': ['45', '35', '56'], 
    'g2': ['67', '76', '65', '50'], 
    'g3': ['8', '70', '96']
}

所需的输出:

For ['akshay', 'swapnil', 'parth','juhi'], weight=45 
For ['gaurav', 'geeta'], weight=70

有人可以帮我吗?...

1 个答案:

答案 0 :(得分:0)

您有三个字典,三个之间的键相同。这是三者之间的主要相互关系因素。因此,在遍历一个字典的同时,您可以使用相同的键从其他字典中获取值。运动和重量之间的相互关系基于它们在相应列表中的相对位置而来。这是通过在一个列表中获取值的索引并在相应列表中的相同位置获取该值来实现的。

根据您的输入和您的输出期望,尝试以下代码:

dic1 = {
    'g1': ['akshay', 'swapnil', 'parth','juhi'],
    'g2': ['megha', 'varun', 'tarun'],
    'g3': ['gaurav', 'geeta']
}
dic2 = {
    'g1': ['hockey', 'basketball', 'football'],
    'g2': ['cricket', 'chess', 'badminton', 'carrom'],
    'g3': ['pool', 'hockey', 'tabletennis']
}
dic3 = {
    'g1': ['45', '35', '56'],
    'g2': ['67', '76', '65', '50'],
    'g3': ['8', '70', '96']
}

for k, v in dic2.items():
    for i, l in enumerate(v):
        print("Group = %s" %(k))
        print("Name List = %s, Sport = %s Weight = %s" %(dic1[k], l, dic3[k][i]))

输出:

Group = g3
Name List = ['gaurav', 'geeta'], Sport = pool Weight = 8
Group = g3
Name List = ['gaurav', 'geeta'], Sport = hockey Weight = 70
Group = g3
Name List = ['gaurav', 'geeta'], Sport = tabletennis Weight = 96
Group = g2
Name List = ['megha', 'varun', 'tarun'], Sport = cricket Weight = 67
Group = g2
Name List = ['megha', 'varun', 'tarun'], Sport = chess Weight = 76
Group = g2
Name List = ['megha', 'varun', 'tarun'], Sport = badminton Weight = 65
Group = g2
Name List = ['megha', 'varun', 'tarun'], Sport = carrom Weight = 50
Group = g1
Name List = ['akshay', 'swapnil', 'parth', 'juhi'], Sport = hockey Weight = 45
Group = g1
Name List = ['akshay', 'swapnil', 'parth', 'juhi'], Sport = basketball Weight = 35
Group = g1
Name List = ['akshay', 'swapnil', 'parth', 'juhi'], Sport = football Weight = 56