如何在列表中修改特定元素(取决于另一个字典),其中列表是字典的值? (Python)

发布于 2025-02-11 05:15:38 字数 979 浏览 1 评论 0原文

给定2个词典:

  1. 学生的ID和每个问题上有多少要点:
user_probstat_dict = {'106095014': [3, 1, 1], '106095029': [3, 1, 0], '106095033': [3, 1, 0]}
  1. 学生的ID和他们被欺骗的问题的数量:
user_cheatprob_dict = {'106095029': [1, 3], '106095033': [3]}

我的目的是将分数更改为0被骗了,例如对于ID“ 106095029”,该学生的原始分数:

'106095029': [3, 1, 0]

应该更改为:

'106095029': [0, 1, 0]

由于并非所有学生都被骗了,因此user_cheatprob_dict不总是包括所有学生IDS。

这是我的代码:

for u, c in user_cheatprob_dict.items():
    for ci in c:
        prob_ind = ci - 1 
            user_probstat_dict.update({u:???})

我的想法是首先循环浏览user_cheatprob_dict,如果有一个学生欺骗了问题,请使用键(Student id = u)并使用更新函数将值替换为0。

???在我的代码中是我不知道如何仅参考特定值的部分(通过索引prob_ind )。 我只知道,如果我想更改整个值,我只需要将0放置在???零件上即可。

谢谢!

Given 2 dictionaries of:

  1. the students' id and how many points they've got on each problem:
user_probstat_dict = {'106095014': [3, 1, 1], '106095029': [3, 1, 0], '106095033': [3, 1, 0]}
  1. the students' id and the number of problem that they've been cheated:
user_cheatprob_dict = {'106095029': [1, 3], '106095033': [3]}

My aim is to change the score into 0 for those problems that have been cheated, like for the id '106095029', this student's original score:

'106095029': [3, 1, 0]

should be changed into:

'106095029': [0, 1, 0]

Since maybe not all of the students have cheated, the user_cheatprob_dict won't always include all of the student ids.

Here's my code:

for u, c in user_cheatprob_dict.items():
    for ci in c:
        prob_ind = ci - 1 
            user_probstat_dict.update({u:???})

My thought is to first loop over the user_cheatprob_dict, if there's a student that has problems cheated, then take the key (student id = u) and use the update function to replace the value into 0.

The ??? in my code is the part where I don't know how to refer to only the specific value(by the index prob_ind)in the list.
I only knew that if I want to change the whole value, I just need to place 0 at the ???part.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疯到世界奔溃 2025-02-18 05:15:38

我有2种解决方案,具体取决于您的需求。另外,这些解决方案是安全的:如果一个或另一个或另一个学生缺少学生的ID,或者被骗的问题的编号与任何问题不符,则不会提出。

1。重新计算您的解决方案
这是更紧凑的,但是由于它重新创建了新的词典,因此它会消耗更多的内存。仅在小样品上使用它。优势在于,您的输入不变。

def recompute(points_dict, cheat_dict):
    """create a new points dict, but doesn't modify the input"""
    return {
        idd: [p * ((i + 1) not in cheat_dict.get(idd, [])) for i, p in enumerate(points)]
        for idd, points in points_dict.items()
    }

与数字乘以数字时,布尔值对应于值1或0(分别为true或false)。

2。直接在输入中替换
不太紧凑,但记忆消耗也少。并且您的输入已更改!

def replace(points_dict, cheat_dict):
    """replace the points in the input: input modified"""
    for idd, numbers in cheat_dict.items():
        if idd in points_dict:
            for num in numbers:
                if num <= len(points_dict[idd]):
                    points_dict[idd][num - 1] = 0

I have 2 solutions, depending on your needs. Also, those solutions are safe: it won't raise if a student's id is missing in one or another dict, or if a cheated problem's number doesn't correspond to any problem.

1. Recompute your solution
This is the more compact one, but it consumes more memory since it recreates a new dictionary. Use it only on small enough samples. The advantage is that you let your input unchanged.

def recompute(points_dict, cheat_dict):
    """create a new points dict, but doesn't modify the input"""
    return {
        idd: [p * ((i + 1) not in cheat_dict.get(idd, [])) for i, p in enumerate(points)]
        for idd, points in points_dict.items()
    }

When multiplied with a number, a boolean corresponds to the value 1 or 0 (respectively for True or False).

2. Replace directly in the input
Less compact, but also less memory consumption. And your input is changed!

def replace(points_dict, cheat_dict):
    """replace the points in the input: input modified"""
    for idd, numbers in cheat_dict.items():
        if idd in points_dict:
            for num in numbers:
                if num <= len(points_dict[idd]):
                    points_dict[idd][num - 1] = 0
不必在意 2025-02-18 05:15:38

您可以使用以下方式使用字典遍历:

user_probstat_dict = {'106095014': [3, 1, 1], '106095029': [3, 1, 0], '106095033': [3, 1, 0]}
user_cheatprob_dict = {'106095029': [1, 3], '106095033': [3]}

for i in list(user_cheatprob_dict.keys()):
    for j in user_cheatprob_dict[i]:
        user_probstat_dict[i][j-1] = 0
print(user_probstat_dict)

输出: 请注意,User_cheatprob_dict的索引被-1取代以获取正确的问题索引

{'106095014': [3, 1, 1], '106095029': [0, 1, 0], '106095033': [3, 1, 0]}

You can use dictionary traversal as follows:

user_probstat_dict = {'106095014': [3, 1, 1], '106095029': [3, 1, 0], '106095033': [3, 1, 0]}
user_cheatprob_dict = {'106095029': [1, 3], '106095033': [3]}

for i in list(user_cheatprob_dict.keys()):
    for j in user_cheatprob_dict[i]:
        user_probstat_dict[i][j-1] = 0
print(user_probstat_dict)

Output: Note that the index of user_cheatprob_dict is offset by -1 to get the correct problem index

{'106095014': [3, 1, 1], '106095029': [0, 1, 0], '106095033': [3, 1, 0]}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文