2016-11-23 14 views
5

से मैं तार (एक .tt फ़ाइल से) की एक सूची है कि इस तरह दिखता है:आबाद शब्दकोश सूची

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

मैं इसे एक शब्दकोश है कि लग रहा है में बदल जाते हैं करना चाहते हैं की तरह:

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

मैं प्रतिस्थापन के बारे में सोच रहा था, लेकिन यह अच्छी तरह से काम नहीं करता है। क्या विभाजक के रूप में टैब स्ट्रिंग '\t' टैग करने का कोई तरीका है?

+3

'है': 'VERB' आपका मतलब है? –

+2

'dict' कुंजी अद्वितीय होना चाहिए – user2728397

उत्तर

4

ऐसा एक सरल dict समझ और एक str.split (तर्क के बिना strip कारतूस पर विभाजन) के साथ

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

परिणाम:

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

संपादित करें: x.split()[0]:x.split()[1] दो बार split करता है, जो इष्टतम नहीं है । यहां अन्य उत्तर यह समझने के बिना बेहतर है।

16

निम्नलिखित का प्रयास करें:

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

आउटपुट:

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

<3 पायथनिक तरीका है <3 – Pitto

+5

' [] 'अनावश्यक है, जनरेटर अभिव्यक्ति ठीक काम करेगी और प्रक्रिया में कम स्मृति का उपयोग करेगी। –

+0

@ ŁukaszRogalski संपादित, टिप्पणी के लिए धन्यवाद :) – ettanany

3

समस्या को हल करने, के बाद से विभाजन विधि डिफ़ॉल्ट रूप से '\t' विभाजन (के रूप में जिम Fasarakis-Hilliard द्वारा बताया एक छोटी रास्ता), हो सकता है:

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

मैंने एक और सरल और क्लासिक दृष्टिकोण भी लिखा।

बहुत pythonic लेकिन आसान नहीं शुरुआती के लिए समझने के लिए:

# Let's start with our word list, we'll call it 'list1' 

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

# Here's an empty dictionary, 'dictionary1' 

dictionary1 = {} 

# Let's 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 

उपयोगी लिंक्स:

  • आप कर सकते हैं

    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 
    

    यहाँ मैं बहुत वर्बोज़ टिप्पणी के साथ एक ही कोड लिखा था इस कोड को जल्दी से कॉपी और पेस्ट करें ताकि यह जांच सके कि यह कैसे काम करता है और यदि आप चाहें तो इसे ट्वीक करें: http://www.codeskulptor.com

  • आप सामान्य रूप में विभाजन और स्ट्रिंग कार्यों के बारे में अधिक जानने के लिए करना चाहते हैं: https://docs.python.org/2/library/string.html
7

str.split के बाद से भी '\t' डिफ़ॉल्ट रूप से चालू विभाजन ('\t' सफेद स्थान माना जाता है), तो आप खिला द्वारा एक कार्यात्मक दृष्टिकोण मिल सकता है dict एक map साथ कि काफी सुंदर लग रहा है:

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

शब्दकोश d अब वांछित रूप में किया जा रहा है:

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

आप केवल पर एक विभाजन की जरूरत है'\t' (जबकि ' ' और '\n' अनदेखी) और अभी भी map दृष्टिकोण का उपयोग करना चाहते हैं, आपको लगता है कि केवल के रूप में '\t' का उपयोग करता functools.partial के साथ एक आंशिक वस्तु बना सकते हैं विभाजक:

from functools import partial 

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

इस, ज़ाहिर है, पैदावार का नमूना सूची का उपयोग कर d के लिए एक ही परिणाम तार।

+0

अधिक अधिक कार्यात्मक हो सकता है, लेकिन यह स्पष्ट रूप से पाइथनिक नहीं है। –

संबंधित मुद्दे