python学习笔记3-dictionary和分词

题目链接

  • words = {} 声明一个字典
  • words.get(w, 0) 查找w的分值,若未找到则返回0
  • A.append()用于向列表追加元素
  • A.sort()按照第一个元素和第二个元素的大小顺序对列表排序
  • A[:k]表示前k个元素
class Solution:
    def topStudents(self, positive_feedback: List[str], negative_feedback: List[str], report: List[str], student_id: List[int], k: int) -> List[int]:
        words = {}
        for w in positive_feedback:
            words[w] = 3
        for w in negative_feedback:
            words[w] = -1
        
        A = []
        for s, i in zip(report, student_id):
            score = sum(words.get(w, 0) for w in s.split())
            A.append([-score, i])
        A.sort()
        return [i for v, i in A[:k]]

你可能感兴趣的:(python基础知识,python,学习,笔记)