Could you tell me how to turn a string into a dictionary in python?
a = "The/AT grand/JJ jury/NN commented/VB
on/In a/AT number/NN of/In other/AP topics/NNS, /,
AMONG/In them/PPO the/AT Atlanta/NP and/CC"
{"AT": [" the","a], "JJ": [" grand"], "NN": [" jury", "number"]," VB": ["commented"]," In": ["on"," of", "among"]," AP": ["other"]," NNS": ["topics"],",": [","], "PPO": [" them"], "NP": [" atlanta"], "CC": [" and"]}
change the above string to the following dictionary. But the limit can only be solved by using str,list,dictionary-related structures
sources of topics and their own ideas
I removed the whitespace of this string to form a list, and then turned the list into a string and then removed the backslash, but after removal, it became a list formed by a single string, and I didn"t know how to form a dictionary of the collection.
related codes
/ / Please paste the code text below (please do not replace the code with pictures)
import re
a = "The/AT grand/JJ jury/NN commented/VB
on/In a/AT number/NN of/In other/AP topics/NNS, /,
AMONG/In them/PPO the/AT Atlanta/NP and/CC"
b = re.findall (" wicked pedigree, a)
c = str (b). Split ("/")
print (c)
what result do you expect? What is the error message actually seen?
my current thinking may also be wrong. I hope I can give you some guidance. Thank you!
b = re.findall(r'(.+?)/(.+?) ', a) -sharp
keys = []
for _b in b:
if _b[1] not in keys:
keys.append(_b[1])
res = {}
for key in keys:
res[key] = [_b[0] for _b in b if _b[1]==key]
bored to read, lambdawn's answer is funny, regular does not write the lost data, lowercase does not convert, the result is not heavy, but also can seriously deceive.
< hr >
is actually a map/reduce problem. It is first split into map, and then aggregated according to the key value:
can be compared, and you can write concise and efficient code in this way, as follows:
def func1():
-sharp /value->key
b = map(lambda x:x.split('/'), a.split())
-sharp key reduceset
c = {}
for v in b:
if v[1] not in c.keys():
c[v[1]] = set()
c[v[1]].add(v[0].lower())
-sharp setlist
d = {k: list(v) for k, v in c.items()}
return d
if you have to follow lambdawn's way of thinking, the code is roughly as follows:
def func2():
b = re.findall(r'(.+?)/(.+?)\s+', a+' ')
keys = []
for _b in b:
if _b[1] not in keys:
keys.append(_b[1])
res = {}
for key in keys:
for _b in b:
if _b[1] == key:
c = res.get(key, [])
if _b[1] == key and _b[0].lower() not in c:
if not c:
res[key] = []
res[key].append(_b[0].lower())
return res
make a comparison of simple performance tests,
def perf_test():
start = time.clock()
for i in xrange(0,10000):
func1()
end = time.clock()
print end-start
for i in xrange(0,10000):
func2()
end = time.clock()
print end-start
result, about double the difference
0.340130693836
0.780941427825
>>> text = '''The/AT grand/JJ jury/NN commented/VB
... on/In a/AT number/NN of/In other/AP topics/NNS ,/,
... AMONG/In them/PPO the/AT Atlanta/NP and/CC'''
...
>>> tuples = [tuple(item.split('/')) for item in text.split()] -sharp
>>> ret_dict = {}
>>> for value, key in tuples: -sharp
... ret_dict.setdefault(key, set()).add(value.lower())
...
>>> ret_dict = {key:list(value) for key, value in ret_dict.items()} -sharp
>>> ret_dict
{'AT': ['the', 'a'], 'JJ': ['grand'], 'NN': ['jury', 'number'], 'VB': ['commented'], 'In': ['of', 'on', 'among'], 'AP': ['other'], 'NNS': ['topics'], ',': [','], 'PPO': ['them'], 'NP': ['atlanta'], 'CC': ['and']}