Python script

60 views
Skip to first unread message

Berry Allen

unread,
Feb 23, 2016, 11:54:58 AM2/23/16
to mumpy
If there are lines written 3 different languages continuously eg: 1st line in English 2nd in Spanish 3rd in french 4th in English and so on.

Can we create a Python Script to delete 1 or 2 types of language lines so that we can have matter written in our language?

nkhalasi

unread,
Feb 24, 2016, 1:29:29 AM2/24/16
to mumpy
PFA a sample idea.
filter_by_language.py
xlation_data.txt

dexterous

unread,
Mar 23, 2016, 4:24:29 PM3/23/16
to mumpy
On Tuesday, February 23, 2016 at 11:54:58 AM UTC-5, Berry Allen wrote:
If there are lines written 3 different languages continuously eg: 1st line in English 2nd in Spanish 3rd in french 4th in English and so on.

Can we create a Python Script to delete 1 or 2 types of language lines so that we can have matter written in our language?

Does it have to be Python? This is a sed 1-liner.

$ cat > input
English
Spanish
German
French
English
Spanish
German
French
English
Spanish
German
French
<Ctrl-D>
$ for i in {1..4}; do echo "Language $i"; sed -nre "${i}~4 p" input; echo ''; done
Language 1
English
English
English

Language 2
Spanish
Spanish
Spanish

Language 3
German
German
German

Language 4
French
French
French

$

- d

dexterous

unread,
Mar 23, 2016, 5:56:10 PM3/23/16
to mumpy
On Wednesday, February 24, 2016 at 1:29:29 AM UTC-5, nkhalasi wrote:
PFA a sample idea.

Naresh babu, what is with all that chaining? You must to be islicing or counting or compressing! ;)

Jokes aside, itertools provides a plethora of functions that let you deal with arbitrary slices/partitions of iterators.
The batteries are include, power up!

- d

PS- Naresh, consider replacement for chain_element, again using iter

def langauge_chain:
    from itertools import cycle, islice
    chain = cycle(('english', 'spanish', 'german', 'french'))

dexterous

unread,
Mar 23, 2016, 5:57:02 PM3/23/16
to mumpy
On Wednesday, March 23, 2016 at 5:56:10 PM UTC-4, dexterous wrote:
PS- Naresh, consider replacement for chain_element, again using iter

s,iter,itertools,

- d 
Reply all
Reply to author
Forward
0 new messages