Posts

Showing posts with the label Dictionary

AttributeError: 'str' Object Has No Attribute 'items'

Answer : You are passing in a string ; headers can't ever be a JSON encoded string, it is always a Python dictionary. The print results are deceptive; JSON encoded objects look a lot like Python dictionary representations but they are far from the same thing. The requests API clearly states that headers must be a dictionary: headers – (optional) Dictionary of HTTP Headers to send with the Request . JSON data is something you'd send as content to another server, not something you'd use to communicate with a Python API. I had this issue and I needed to make the header with a content type and pass in a data element as json. import requests import json headerInfo = {'content-type': 'application/json' } payload = {'text': 'okay!!!', 'auth_token': 'aasdasdasdasd'} jLoad = json.dumps(payload) r = requests.post('http://example.com:3030/widgets/init', headers=headerInfo, data=jLoad) print r.text...

Adding Dictionaries Together, Python

Answer : If you're interested in creating a new dict without using intermediary storage: (this is faster, and in my opinion, cleaner than using dict.items()) dic2 = dict(dic0, **dic1) Or if you're happy to use one of the existing dicts: dic0.update(dic1) Here are quite a few ways to add dictionaries. You can use Python3's dictionary unpacking feature. ndic = {**dic0, **dic1} Or create a new dict by adding both items. ndic = dict(dic0.items() + dic1.items()) If your ok to modify dic0 dic0.update(dic1) If your NOT ok to modify dic0 ndic = dic0.copy() ndic.update(dic1) If all the keys in one dict are ensured to be strings ( dic1 in this case, of course args can be swapped) ndic = dict(dic0, **dic1) In some cases it may be handy to use dict comprehensions (Python 2.7 or newer), Especially if you want to filter out or transform some keys/values at the same time. ndic = {k: v for d in (dic0, dic1) for k, v in d.items()} >>> dic0 = {...

Can Python Dictionary Comprehension Be Used To Create A Dictionary Of Substrings And Their Locations?

Answer : The problem is that v[0] depends on the length or v[1] , which means that either the operation to generate v[1] would have to operate twice, or that the dictionary would have to be iterated over in order to fill in v[0] to replace the dummy value included the first time. Another problem is that dict comprehensions expect the entire key and value to be available immediately, which means that you would have to run a list comprehension to get all the indexes of the character, which means that the entire operation becomes O(n 2 ). The only optimization I would make would be to replace the creation of d so that you don't need to check for key containment. d = collections.defaultdict(lambda: [0, []]) It is scary, but (I added just offsets, number of occurrences you may get from list of offsets). Yes, it may be done In [83]: my_str = 'abcdabcxdabc' In [84]: n=3 In [85]: {substr: [my_str.replace(substr, ' '*n, c).index(substr) ...