-2

A variant of this question has been asked in the past. However, I wasn't able to get the responses working for me. This is a sample dict I have:

{rdflib.term.URIRef(u'<http://purl.org/net/ontology#isGivenDrugA>'): rdflib.term.Literal(u'<http://purl.org/net/ontology#dose1>')}
{rdflib.term.URIRef(u'<http://purl.org/net/ontology#isGivenDrugB>'): rdflib.term.Literal(u'<http://purl.org/net/ontology#dose2>')}
{rdflib.term.URIRef(u'<http://purl.org/net/ontology#isGivenDrugC>'): rdflib.term.Literal(u'<http://purl.org/net/ontology#dose3>')}

I want to rename the first term in each of the dicts to isNotGivenDrugA or B or C. For instance:

{rdflib.term.URIRef(u'<http://purl.org/net/ontology#isNotGivenDrugA>'): rdflib.term.Literal(u'<http://purl.org/net/ontology#dose1>')}

How can I do so?

This is the code I have so far:

for s in notGivenDrugList:
        print ('s: ',s)
        for k,v in s.iteritems():
            print k

k gives me the first term of each dict. If I assign it a value based on k.replace, that doesn't work as keys of dict are immutable and iteritems only creates copies of the key.

kurious
  • 1,024
  • 10
  • 29

1 Answers1

1

You can't. You will need to recreate the dictionary items with the desired key.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
  • I understand that bit (hence the reference to immutability of the keys in the question). The problem is, I wouldn't know all the elements of the list as the data is being parsed from a database. What is an elegant way to program this? – kurious Apr 18 '16 at 07:38
  • There isn't one. You'd have to load the database and then process the data afterwards. – Ignacio Vazquez-Abrams Apr 18 '16 at 07:43