Getting rid of duplicates - python-2.7

Can someone help?
I have a file like this:
file = """he is a good man
she is a beautiful woman
this is a clever student
he is a bad neighbour"""
And I want to mark the adjectives in the file, so I extracted them in a list and I want to replace them in the original file in another format, like between brackets. So the list adjectives looks like this
ad = "good, bad, clever, beautiful,"
I tried this
for line in file.splitlines():
for a in ad.split(','):
if a in line:
newline = line.replace(s, '[' + s + ']')
result = file.replace(line, newline)
print result
this gives me this result with duplicates:
he is a [good] man
she is a beatiful woman
this is a clever student
he is a bad neighbor
he is a good man
she is a[ beatiful] woman
this is a clever student
he is a bad neighbor
he is a good man
she is a beatiful woman
this is a[ clever] student
he is a bad neighbor
he is a good man
she is a beatiful woman
this is a clever student
he is a[ bad] neighbor
while I expect result like this
he is a [good] man
she is a [beautiful] woman
this is a [clever] student
he is a [bad] neighbour

The problem is your loop. You aren't splitting your adjectives well and the splitline is unnecessary if you are going to use replace. So, your code should be as follows:
adj = [word.strip() for word in ad.split(",") if word != ""]
for a in adj:
if a in file:
file = file.replace(a, '[' + a + ']')
print file

Related

regexp, how to match longer strings first?

There must be an option/flag for this I missed with matlab:
I want to use regular expressions to match to my given string, but multiple matches are possible. I want them sorted to first match the longer ones, before the shorter ones.
How can this be achieved?
regexpi('A quick brown fox jumps over the lazy dog.','quick|the|a','match','once')
%returns 'A', would like it to return 'quick'
Maybe you can try the following code
% match all possible key words, don't use argument 'once' in `regexpi()`
v = regexpi('A quick brown fox jumps over the lazy dog.','quick|the|a','match');
% calculate the lengths of matched words
lens = cellfun(#length,v);
% output the longest word
v{lens == max(lens)}
such that
ans = quick
You could do
>> regexpi('A quick brown fox jumps over the lazy dog.','.*?(quick)|.*?(the)|.*?(a)','tokens','once')
ans =
1×1 cell array
{'quick'}
but that's pretty ugly. Another solution, which is a smidge less ugly, is
>> str = "A quick brown fox jumps over the lazy dog.";
>> list = ["quick" "the" "a"];
>> list(find(arrayfun(#(x)contains(str,x), list), 1))
ans =
"quick"
I think I like Thomas' solution the most.

QString - parsing QString with geoordinates

I work in Qt Creator (Community) 5.5.1. For example, I have
string="44° 36' 14.2\" N, 33° 30' 58.6\" E, 0m"
of QString. I know, that I must parse it, but i don't know how, because I have never faced with the problem like it. From our string I want to get some other smaller strings:
cgt = "44"; cmt = "36"; cst = "14.2"
cgg = "33"; cmg = "30"; csg = "58.6"
What must I do for working my programm how I said?
I need real code. Thanks.
The simplest way to start would be string.split(' ') - that would yield the list of the string components that were separated by the space character (' '). If you're sure the string will always be formatted exactly like this, you can first remove all the special characters (° and so on).
Then analyze the resulting QStringList. Again, if the format is fixed, you can check that the number of list items matches the expected number, and then get degrees as list[0], minutes as ``list[1]` and so on.
Another alternative would be to use QRegExp for parsing the string (splitting it into substrings based on regex), but I find it too complicated for use cases where split works just as well.
"I need code" is not the kind of question you should be asking, SO is about "gimme knowledge" not about "do my work" questions. A good question should demonstrate your effort to solve the problem, so people can tell you what you are doing wrong. Not only does your question lack any such effort, but you didn't expend any even when Devopia did half of the work for you. Keep that in mind for your future questions.
struct G {
double cgt, cmt, cst, cgg, cmg, csg;
};
G parse(QString s) {
QStringList list = s.split(QRegExp("[^0-9.]"), QString::SkipEmptyParts);
G g;
g.cgt = list.at(0).toDouble();
g.cmt = list.at(1).toDouble();
g.cst = list.at(2).toDouble();
g.cgg = list.at(3).toDouble();
g.cmg = list.at(4).toDouble();
g.csg = list.at(5).toDouble();
return g;
}

how to split a C++ string to get whole string individually and some parts/characters of it

My question is, how can I split a string in C++? For example, I have `
string str = "[ (a*b) + {(c-d)/f} ]"
It need to get the whole expression individually like [,(,a,*,b,......
And I want to get the brackets only like [,(,),{,(,),},] on their proper position
How can I do these with some easy ways
This is called lexical analysis (getting tokens from some sequence or stream of characters) and should be followed by parsing. Read e.g. the first half of the Dragon Book.
Maybe LL parsing is enough for you....
There are many tools for that, see this question (I would suggest ANTLR). You probably should build some abstract syntax tree at some point.
But it might not worth the effort. Did you consider embedding some scripting language in your application, e.g. lua (see this and this...), or gnu guile, python, etc...
Here is a way I got to do this,
string expression = "[ (a*b) + {(c-d)/f} ]" ;
string token ;
// appending an extra character that i'm sure will never occur in my expression
// and will be used for splitting here
expression.append("~") ;
istringstream iss(expression);
getline(iss, token, '~');
for(int i = 0 ; i < token.length() ; i++ ) {
if(token[i] != ' ' ) {
cout<<token[i] << ",";
}
}
Output will be: [,(,a,*,b,),+,{,(,c,-,d,),/,f,},],

How to show regex output in pair in c++?

I dont know if it makes any sense or not but here it is
Is there a way that i can get two words from a regex result each time?
supose i have a text file which contains an string such as the following :
Alex Fenix is an Engineer who works for Ford Automotive Company. His
Personal ID is <123456>;etc....
basically if i use \w i would get a list of :
Alex
Fenix
is
an
Engineer
and etc
They are all separated by white space and punctuation marks
what i am asking is , whether there is a way to have a list such as :
Alex Fenix
is an
Engineer who
works for
Ford Automotive
Company His
Personal ID
is 123456
How can i achieve such a format?
Is it even possible or should i store those first results in an array and then iterate through them and create the second list?
By the way please note that the item Alex Fenix is actually an abstraction of a map or any container like that.
The reason i am asking is that i am trying to see if there is any way that i can directly read a file and apply a regex on it and get this second list without any further processing overhead
(I mean reading into a map or string , then iterating through them and creating pairs of the tokens and then carry on what ever is needed )
Try this regex
\w \w
It will match any word followed by a space and another word.
Although you can achieve such a format relatively easy without using a regex. Take a look at this for instance:
#include <iostream>
#include <sstream>
#include <string>
#include <algorithm>
int main() {
std::string s("Alex Fenix is an Engineer who works for Ford Automotive Company. His Personal ID is <123456>");
// Remove any occurences of '.', '<' or '>'.
s.assign(begin(s), std::remove_if(begin(s), end(s), [] (const char c) {
return (c == '.' || c == '<' || c == '>');
}));
// Tokenize.
std::istringstream iss(s);
std::string t1, t2;
while (iss >> t1 >> t2) {
std::cout << t1 << " " << t2 << std::endl;
}
}
Output:
Alex Fenix
is an
Engineer who
works for
Ford Automotive
Company His
Personal ID
is 123456

How to Reverse a given sentence (string) in C++?

Example: if the input was DOGS LIKE CATS
output- CATS LIKE DOGS
consider that I have to use only : If-else conditions, while & for loops, Arrays, strings and Functions. NOT strings functions, Pointers & Dynamic memory allocation & structures.
Spaces need to be the same as the example as well.
I tried to do the following but it doesnt work can you help please?
void revSent(char str[]){
char temp[100];
int k;
for (i=sentenceSize ; i>0 ; i--)
for (k=0 ; k<sentenceSize ; k++)
temp[k]=str[i];
for (k=0 ; k<sentenceSize ; k++)
if (temp[k]!=' ')
for (i=k ; i>0 ; i--)
printf("%c", temp[i]);
}
It's easy to do this in-place, without any additional data structures:
reverse the whole string: DOGS LIKE CATS -> STAC EKIL SGOD
reverse each word in the string: STAC EKIL SGOD -> CATS LIKE
DOGS
Hint: you can use the same function for both (1) and (2).
You could implement the following to arrive at a solution:
Separate the sentence into a list of words.
Reverse this list.
Concat this list together to form the sentence.
If you define a word as a whitespace-delimited token then the following will do:
std::vector<std::string> sentence;
std::copy(std::istream_iterator<std::string>(std::cin),
std::istream_iterator<std::string>(),
std::back_inserter(sentence));
std::reverse(sentence.begin(), sentence.end());
In essence you want to start with the definition of a word, then place in a container your words, and finally use std::reverse() to reverse them.
For an algorithms homework your instructor probably won't be satisfied with this. You want to create a function that splits a sentence into words. You can, of course, work with pointers within the same string -- and that may well be the intent of your instructor, but if that isn't what you must then I personally find working with a container easier.
I'll give a hint: since you can't use data structures you can't directly use Paul or OJ's method. BUT, recursive function calling would form a stack.
Break the sentence into words
In order, push each word onto a stack
Pop each item off the stack and print out/add to list/write to file/whatever.
Voila!
who says the STL isn't useful?
Depending on how you do this, there are different ways to attack this.
my way is just:
while (original_string isn't empty){
take first word
prepend to reversed string
}
Here's the C++ solution (using find and substr, two very useful string functions)
using namespace std;
string word_reverse(string original){
string reverse_string;
if (original.find(' ')!=string::npos){
do{
int pos=original.find(' ');
//prepend word
reverse_string=original.substr(0,pos)+' '+reverse_string;
//remove word from original,considering the found whitespace
original=original.substr(pos+1,original.length()-(pos+1));
}while(original.find(' ')!=string::npos);
//don't forget the last word!
return original+' '+reverse_string;
}
else{//no whitespace: return original text
return original;
}
}