简体   繁体   English

字典元组中的索引 python

[英]index in a tuple of a dictionaries python

I have a tuple which its elements are dictionaries.我有一个元组,它的元素是字典。 ie IE

tuple=({"code":101,"assignment":'math',"credits":2},
       {"code":102,"assignment":'physics',"credits":3},
       {"code":103,"assignment":'chemistry',"credits":2},
       {"code":104,"assignment":'biology',"credits":5},
       {"code":105,"assignment":'science',"credits":1}

Then I want to check how many credits each student has, but first I would like to have a list the index where each code is然后我想检查每个学生有多少学分,但首先我想列出每个代码所在的索引

student1=[101,103]
student2=[102,104,105]

So I would like have所以我想有

student1list=[1,3]
student1credits=4
student2list=[2,4,5]
student2credits=9

I can get those credits with 2 for like this我可以像这样用 2 获得这些学分

s1=[]
for i in range(len(student1)) :
  for j in range(len(tuple)) :
    if  student1[i]==tuple[j]['code'] :
      s1.append(tuple[j]['credits'])

student1credits=0
for k in range(len(s1)):
  student1credits=student1credits+s1[k]

print(student1credits)

But no idea of how to get the indexes但不知道如何获取索引

This feels like something where pandas would help out a lot.这感觉就像 pandas 会有很大帮助。

import pandas as pd

tuple=({"code":101,"assignment":'math',"credits":2},
{"code":102,"assignment":'physics',"credits":3},
{"code":103,"assignment":'chemistry',"credits":2},
{"code":104,"assignment":'biology',"credits":5},
{"code":105,"assignment":'science',"credits":1})

my_data = pd.DataFrame(tuple)

my_data.head()
code代码 assignment任务 credits学分
0 0 101 101 math数学 2 2
1 1 102 102 physics物理 3 3
2 2 103 103 chemistry化学 2 2
3 3 104 104 biology生物学 5 5
4 4 105 105 science科学 1 1

Look up the codes for a student查找学生的代码

student1=[101,103]
keep = my_data.code.isin(student1)
my_data[keep]
code代码 assignment任务 credits学分
0 0 101 101 math数学 2 2
2 2 103 103 chemistry化学 2 2

Get just the index只获取索引

student1=[101,103]
keep = my_data.code.isin(student1)
my_data[keep].index.values

array([0, 2], dtype=int64)数组([0, 2],dtype=int64)

ps: Sorry, these index start at 0 instead of 1...hope that is ok. ps:抱歉,这些索引从 0 而不是 1 开始...希望没关系。 :) :)

An Extension...一个扩展...

Let's store our student-class relationship in pandas too, and do the look up/sum of credits.让我们也将我们的学生-班级关系存储在 pandas 中,并进行学分的查找/求和。

student_class_relationships = pd.DataFrame(
    {'student_id': [1, 1, 2, 2, 2], 
     'code': [101, 102, 103, 104, 105]}
)

my_data \
    .merge(student_class_relationships, on='code') \
    .groupby('student_id') \
    .agg({'credits': 'sum'})
student_id学生卡 credits学分
1 1 5 5
2 2 8 8

You can use list comprehension with enumerate .您可以将列表推导与enumerate一起使用。

tuple_credits = ({"code":101,"assignment":'math',"credits":2},{"code":102,"assignment":'physics',"credits":3},{"code":103,"assignment":'chemistry',"credits":2},{"code":104,"assignment":'biology',"credits":5},{"code":105,"assignment":'science',"credits":1})

code_to_idx = {x['code']: i for i, x in enumerate(tuple_credits)}

def idx_and_credits(student):
    idx = [code_to_idx[c] for c in student]
    cred = sum(tuple_credits[i]['credits'] for i in idx)
    return idx, cred

student1list, student1credits = idx_and_credits([101, 103])
student2list, student2credits = idx_and_credits([101, 103, 101, 105])

print(student1list, student1credits) # [0, 2] 4
print(student2list, student2credits) # [0, 2, 0, 4] 7

In Python, index starts with 0, so student1list would be [0, 2] instead of [1, 3] .在 Python 中,索引从 0 开始,因此student1list将是[0, 2]而不是[1, 3] Also I renamed tuple to tuple_credits , as tuple is a built-in function.我还将tuple组重命名为tuple_credits ,因为tuple是内置的 function。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM