dictionary - Python script to map one string to another string -


i have file called file1.txt contents like:

file1.txt

python general-purpose, interpreted, interactive, object-oriented , high-level programming language. python developed guido van rossum in late eighties , nineties @ national research institute mathematics , computer science in netherlands. python derived many other languages, including abc, modula-3, c, c++, algol-68, smalltalk , unix shell , other scripting languages. python copyrighted. perl, python source code available under gnu general public license (gpl).

i want map string eg. third sentence in file string -"python designed highly readable uses english keywords " .

how mapping can done in python? got know dictionary equivalent hashmap in python can dictionary used map 1 string another??

i tried like:

f=open("log5.txt") dict = {'f.readline()': 'python designed highly readable uses english keywords frequently'} print(f.readline()) print(dict['f.readline()']) 

but above program maps first sentence , there better , efficient way write above program???

alright, 2 easy solutions: .split(...) , re.(...)

text = "python ... license (gpl)."  # add periods, isn't efficient way (use regex or tokenizer better results works) def add_periods(sentences)    return [sentence + "." sentence in periodless_sentences]  # have no idea why you'd want def map_to_sentence_index(sentences):    return {i: (s + ".") (s, i) in enumerate(sentences)}  # split periodless_sentences = str.split('. ')  # regex import re sentences = re.split(r' *[\.\?!][\'"\)\]]* *', text) 

in python map means map(func, lst) func function want apply every element of lst. not describing. map reserved keyword.

map(len, ["a", "bb", "ccc"]) => [1, 2, 3] 

honestly question makes little sense in context of python , looks copy-pasted textbook. please don't use stackoverflow homework solutions site. said, here's solution lol


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

java - Android – MapFragment overlay button shadow, just like MyLocation button -