1

I am currently bringing a list down from an api and changing the names based on severity levels

Right now my code to do this is

priname = list((item['fields']['priority']['name']) for item in data['issues'])
priname = [w.replace('Trivial', 'Low')
            .replace('Minor', 'Low')
            .replace('Moderate', 'Medium')
            .replace('Major', 'High')
            .replace('Critical', 'High')
            .replace('Blocker', 'Emergency')for w in priname]

So I am just replacing strings in the list to keywords that generalize the content. When I do this to larger lists, it takes forever to return the altered list which i realize means this is not an efficient way of doing this at all.

Can anyone point me in a direction where i can streamline this find and replace?

EDIT:

Priname=[
'Critical',
'Moderate',
'Major',
'Moderate',
'Moderate', 
'Critical', 
'Moderate', 
'Moderate', 
'Moderate', 
'Blocker', 
'Critical', 
'Moderate', 
'Moderate', 
'Major', 
'Moderate', 
'Critical'
]
5
  • 1
    Use list-comp ([... for x in ...]) rather than list() + generator-expression. It's more Pythonic. Commented Nov 30, 2018 at 17:33
  • Can you post data or priname so we can see what we're working with? Commented Nov 30, 2018 at 17:34
  • I posted priname, i don't think there is a need to post data though? im just trying to change this list, it shouldnt matter where it comes from right? to be clear i am able to change the list with the code i have, just not efficiently Commented Nov 30, 2018 at 17:38
  • 1
    @Mfreeman can there be a situation where an item in Priname can have two replacements applied to it, or is it strictly one replacment operation (at most) per element? Commented Nov 30, 2018 at 17:41
  • I see, yeah there would be only one replacement, the whole api is pretty uniform Commented Nov 30, 2018 at 17:45

3 Answers 3

6

You could use a dictionary:

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)

Output

['High', 'Medium', 'High', 'Medium', 'Medium', 'High', 'Medium', 'Medium', 'Medium', 'Emergency', 'High', 'Medium', 'Medium', 'High', 'Medium', 'High']
Sign up to request clarification or add additional context in comments.

3 Comments

This assumes that there's at most one replacement per element in priname and that the elements match the replacements perfectly. I think we can assume that given the structure of OP's example, so +1
Also, table.get(e, e) over table[e] would return the original string if there's no replacment.
This example is definitely cleaner than the code i have, but It seems to be returning in the same amount of time though, i'm not sure anything else will be faster.
1

One idea that comes to mind is to, rather than using a for loop, convert the list into a numpy array and use the numpy.where function:

    w = numpy.array(w)
    ind = numpy.where(w==list_elements_you_are_looking_for)
    w[ind] = new_value

Comments

1

You can use a dictionary and assign multiple values for each key:

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]

Returns:

['High', 'Medium', 'High', 'Medium', 'Medium', 'High', 'Medium', 'Medium', 'Medium', 'Emergency', 'High', 'Medium', 'Medium', 'High', 'Medium', 'High']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.