-
慕容森
你可以使用字典:priname = [ 'Critical', 'Moderate', 'Major', 'Moderate', 'Moderate', 'Critical', 'Moderate', 'Moderate', 'Moderate', 'Blocker', 'Critical', 'Moderate', 'Moderate', 'Major', 'Moderate', 'Critical']table = {'Trivial': 'Low', 'Minor': 'Low', 'Moderate': 'Medium', 'Major': 'High', 'Critical': 'High', 'Blocker': 'Emergency'}result = [table[e] for e in priname]print(result)输出['High', 'Medium', 'High', 'Medium', 'Medium', 'High', 'Medium', 'Medium', 'Medium', 'Emergency', 'High', 'Medium', 'Medium', 'High', 'Medium', 'High']
-
慕码人2483693
您可以使用字典并为每个键分配多个值:replacements = {'Low': ['Trivial','Minor'], 'Medium': ['Moderate'], 'High': ['Major','Critical'], 'Emergency': ['Blocker']}Priname=['Critical','Moderate','Major','Moderate','Moderate', 'Critical', 'Moderate', 'Moderate','Moderate', 'Blocker', 'Critical', 'Moderate', 'Moderate', 'Major', 'Moderate', 'Critical']Priname = [k for i in Priname for k, v in replacements.items() if i in v]返回:['High', 'Medium', 'High', 'Medium', 'Medium', 'High', 'Medium', 'Medium', 'Medium', 'Emergency', 'High', 'Medium', 'Medium', 'High', 'Medium', 'High']
-
临摹微笑
想到的一个想法是,而不是使用 for 循环,将列表转换为 numpy 数组并使用 numpy.where 函数: w = numpy.array(w) ind = numpy.where(w==list_elements_you_are_looking_for) w[ind] = new_value