Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

python - How to Compare two dictionaries and substitute a string from the value

I have 2 dictionary

  • I need to check the values of keys of d2 in d1 by passing code, Name, module
  • if all matches then extract details from the d2 then split by | and take the length of it
  • Then check the length of above and count(%s) from d1
  • if both are same then extract MessageRole and construct a Message from d1
d1 = [ { "Name": "Country", "module": "Request", "code": 11101, "Message": "Data Approved %s", "MessageRole": "Country User" }, { "Name": "Country", "module": "Request", "code": 11102, "Message": "Data Granted %s", "MessageRole": "Country User" }, { "Name": "Country", "module": "Request", "code": 11103, "Message": "Data Denied %s %s", "MessageRole": "Country User" }, { "Name": "Country", "module": "Request", "code": 11201, "Message": "Data Request %s %s %s", "MessageRole": "Data Owner" } ]
d2 = { "Name" : "Country", "module": "Request", "code" : 11201, "details": "A|B|C" } 

My code is below

Role = ''
Message = ''
for each in d1:
    #I need to check the values of keys of `d2` in `d1` by passing `code`, `Name`, `module`
    if each["code"] == d2['code'] and 
            each["Name"] == d2['Name'] and 
            each["module"] == d2['module']:     
        #if all matches then extract `details` from the `d2` then split by `|` and take the length of it
        details = tuple(d2['details'].split('|'))
        print(details)
        variables = len(details)
        print(variables)
        # Then check the length of above and `count(%s)` from d1 
        if variables == each["Message"].count('%s'):
            # if both are same then extract `MessageRole` and construct a `Message` from d1
            Role = each["MessageRole"]
            Message = each["Message"] % ("<i>%s</i>"  % details)

Expected out is below

My Role will be having

Data Owner

My Message will be having

Data Request <i>A</i> <i>B</i> <i>C</i>
question from:https://stackoverflow.com/questions/65847626/how-to-compare-two-dictionaries-and-substitute-a-string-from-the-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Change

Message = each["Message"] % ("<i>%s</i>"  % details)

to

Message = each["Message"]  % tuple("<i>%s</i>" % detail for detail in details)

and it will work as expected.

Alternatively, you could use:

Message = each["Message"].replace("%s","<i>%s</i>") % details

which is a bit shorter.

The problem is that (with your date) details has 3 items so ("<i>%s</i>" % details) doesn't make sense. You only need the first detail for that one string, hence the not all arguments converted during string formatting error message.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...