Filling in the dictionary from the list

I have a list of lines (from a .tt file) that looks like this:

 list1 = ['have\tVERB', 'and\tCONJ', ..., 'tree\tNOUN', 'go\tVERB'] 

I want to turn it into a dictionary that looks like this:

 dict1 = { 'have':'VERB', 'and':'CONJ', 'tree':'NOUN', 'go':'VERB' } 

I was thinking of a replacement, but that is not so good. Is there a way to mark tab string '\t' as a delimiter?

+5
source share
4 answers

Try the following:

 dict1 = dict(item.split('\t') for item in list1) 

Output:

 >>>dict1 {'and': 'CONJ', 'go': 'VERB', 'tree': 'NOUN', 'have': 'VERB'} 
+16
source

Since str.split also defaults to '\t' ( '\t' is considered a space), you can get a functional dict approach with map , which looks pretty elegant:

 d = dict(map(str.split, list1)) 

Now that the d dictionary is in the desired form:

 print(d) {'and': 'CONJ', 'go': 'VERB', 'have': 'VERB', 'tree': 'NOUN'} 

If you only want to split into '\t' (while ignoring ' ' and '\n' ) and still want to use the map approach, you can create a partial object with functools.partial that uses '\t' as a separator:

 from functools import partial # only splits on '\t' ignoring new-lines, white space etc tabsplit = partial(str.split, sep='\t') d = dict(map(tabsplit, list1)) 

this of course gives the same result for d using an example list of strings.

+7
source

do it with a simple understanding of dict and str.split (without arguments strip str.split into spaces)

 list1 = ['have\tVERB', 'and\tCONJ', 'tree\tNOUN', 'go\tVERB'] dict1 = {x.split()[0]:x.split()[1] for x in list1} 

result:

 {'and': 'CONJ', 'go': 'VERB', 'tree': 'NOUN', 'have': 'VERB'} 

EDIT: x.split()[0]:x.split()[1] does split twice, which is not optimal. The other answers here do it better without understanding the concepts.

+4
source

A short way to solve the problem, since the default split method breaks '\t' (as Jim Fasarakis-Hilliard pointed out) could be:

 dictionary = dict(item.split() for item in list1) print dictionary 

I also wrote a simpler and classic approach.

Not very pythonic, but easy to understand for beginners:

 list1 = ['have\tVERB', 'and\tCONJ', 'tree\tNOUN', 'go\tVERB'] dictionary1 = {} for item in list1: splitted_item = item.split('\t') word = splitted_item[0] word_type = splitted_item[1] dictionary1[word] = word_type print dictionary1 

Here I wrote the same code with very detailed comments:

 # Let start with our word list, we'll call it 'list1' list1 = ['have\tVERB', 'and\tCONJ', 'tree\tNOUN', 'go\tVERB'] # Here an empty dictionary, 'dictionary1' dictionary1 = {} # Let start to iterate using variable 'item' through 'list1' for item in list1: # Here I split item in two parts, passing the '\t' character # to the split function and put the resulting list of two elements # into 'splitted_item' variable. # If you want to know more about split function check the link available # at the end of this answer splitted_item = item.split('\t') # Just to make code more readable here I now put 1st part # of the splitted item (part 0 because we start counting # from number 0) in "word" variable word = splitted_item[0] # I use the same apporach to save the 2nd part of the # splitted item into 'word_type' variable # Yes, you're right: we use 1 because we start counting from 0 word_type = splitted_item[1] # Finally I add to 'dictionary1', 'word' key with a value of 'word_type' dictionary1[word] = word_type # After the for loop has been completed I print the now # complete dictionary1 to check if result is correct print dictionary1 

Useful links:

+3
source

Source: https://habr.com/ru/post/1260217/


All Articles