Question on Joining of list

S

SUBHABRATA

Dear Group,
I am trying the following code line:
def try2(n):
a1=raw_input("PRINT A STRING:")
a2=a1.split()
a3="God Godess Heaven Sky"
for x in a2:
a4=a3.find(x)
if a4>-1:
a5=a3[a4]
print a5
elif a4<0:
a6=x
print "It is not found"
print a6
else:
print "Error"
s=a5+" "+a6
print s

Here, if I put a string like:
Petrol Helium Heaven Sky
In s it is giving me S Helium
But I am looking for an output of a5 and a6 concatenating all its
values not the last ones. Can you suggest me any help? Am I missing
any minor point?
Best Regards,
Subhabrata.
 
M

Marc 'BlackJack' Rintsch

def try2(n):
a1=raw_input("PRINT A STRING:")
a2=a1.split()
a3="God Godess Heaven Sky"
for x in a2:
a4=a3.find(x)
if a4>-1:
a5=a3[a4]
print a5
elif a4<0:
a6=x
print "It is not found"
print a6
else:
print "Error"
s=a5+" "+a6
print s

Here, if I put a string like:
Petrol Helium Heaven Sky
In s it is giving me S Helium
But I am looking for an output of a5 and a6 concatenating all its
values not the last ones. Can you suggest me any help? Am I missing
any minor point?

Maybe you should describe what the input looks like and what output you
want to have and how the input and output are connected. In words, not in
not very clear code with "numbered names". That's a silly idea and makes
understanding the code very hard. Please use meaningful names!

Ciao,
Marc 'BlackJack' Rintsch
 
P

Peter Otten

SUBHABRATA said:
I am trying the following code line:

def try2(n):
user_line = raw_input("PRINT A STRING:")
user_words = user_line.split()
my_line = "God Godess Heaven Sky"
for word in user_words:
pos = my_line.find(word)
if pos >- 1:
first_char = my_line[pos]
print first_char
elif pos < 0:
missing_word = word
print "It is not found"
print missing_word
else:
print "Error"
s = first_char + " " + missing_word
print s

try2(1)

Do you recognise your code? With that jumpstart you should find the error in
no time ;)
Here, if I put a string like:
Petrol Helium Heaven Sky
In s it is giving me S Helium
But I am looking for an output of a5 and a6 concatenating all its
values not the last ones. Can you suggest me any help? Am I missing
any minor point?

Yes, use meaningful variables. They won't make your code magically correct
but will make it a lot easier to pinpoint mistakes and false assumptions --
for you and others.

Peter
 
S

SUBHABRATA

Sorry if I didn't say that.
The input is a string "Petrol Helium Heaven Sky"
Now, in a3 it is "God Goddess Heaven Sky" is there,
it is matching Heaven and Sky but not Petrol and Helium as they are
not in a3.
Now, as per the code it is giving me an output "S" of "Sky" and
"Helium"
But I was looking for an output of "H S Petrol Helium" and not "S
Helium" meaning all the values of a5 and a6 will be concatenated in s.
Best Regards,
Subhabrata..
def try2(n):
a1=raw_input("PRINT A STRING:")
a2=a1.split()
a3="God Godess Heaven Sky"
for x in a2:
a4=a3.find(x)
if a4>-1:
a5=a3[a4]
print a5
elif a4<0:
a6=x
print "It is not found"
print a6
else:
print "Error"
s=a5+" "+a6
print s

Here, if I put a string like:
Petrol Helium Heaven Sky
In s it is giving me S Helium
But I am looking for an output of a5 and a6 concatenating all its
values not the last ones. Can you suggest me any help? Am I missing
any minor point?

Maybe you should describe what the input looks like and what output you
want to have and how the input and output are connected. In words, not in
not very clear code with "numbered names". That's a silly idea and makes
understanding the code very hard. Please use meaningful names!

Ciao,
Marc 'BlackJack' Rintsch
 
S

SUBHABRATA

Thanx Peter,
I would change my variables next time I would post. And obviously,
thanx for your solution. I am reviewing it, I was also trying out some
solutions.
Best Regards,
Subhabrata.

Peter said:
SUBHABRATA said:
I am trying the following code line:

def try2(n):
user_line = raw_input("PRINT A STRING:")
user_words = user_line.split()
my_line = "God Godess Heaven Sky"
for word in user_words:
pos = my_line.find(word)
if pos >- 1:
first_char = my_line[pos]
print first_char
elif pos < 0:
missing_word = word
print "It is not found"
print missing_word
else:
print "Error"
s = first_char + " " + missing_word
print s

try2(1)

Do you recognise your code? With that jumpstart you should find the error in
no time ;)
Here, if I put a string like:
Petrol Helium Heaven Sky
In s it is giving me S Helium
But I am looking for an output of a5 and a6 concatenating all its
values not the last ones. Can you suggest me any help? Am I missing
any minor point?

Yes, use meaningful variables. They won't make your code magically correct
but will make it a lot easier to pinpoint mistakes and false assumptions --
for you and others.

Peter
 
S

SUBHABRATA

Hi Peter,
In your code s would print first_char(of the last word)+"
"+missing_word(the last word) I was looking all.
Best Regards,
Subhabrata.
Sorry if I didn't say that.
The input is a string "Petrol Helium Heaven Sky"
Now, in a3 it is "God Goddess Heaven Sky" is there,
it is matching Heaven and Sky but not Petrol and Helium as they are
not in a3.
Now, as per the code it is giving me an output "S" of "Sky" and
"Helium"
But I was looking for an output of "H S Petrol Helium" and not "S
Helium" meaning all the values of a5 and a6 will be concatenated in s.
Best Regards,
Subhabrata..
def try2(n):
a1=raw_input("PRINT A STRING:")
a2=a1.split()
a3="God Godess Heaven Sky"
for x in a2:
a4=a3.find(x)
if a4>-1:
a5=a3[a4]
print a5
elif a4<0:
a6=x
print "It is not found"
print a6
else:
print "Error"
s=a5+" "+a6
print s

Here, if I put a string like:
Petrol Helium Heaven Sky
In s it is giving me S Helium
But I am looking for an output of a5 and a6 concatenating all its
values not the last ones. Can you suggest me any help? Am I missing
any minor point?

Maybe you should describe what the input looks like and what output you
want to have and how the input and output are connected. In words, not in
not very clear code with "numbered names". That's a silly idea and makes
understanding the code very hard. Please use meaningful names!

Ciao,
Marc 'BlackJack' Rintsch
 
P

Peter Otten

SUBHABRATA said:
Thanx Peter,
I would change my variables next time I would post.

No, you should use meaningful variable names when you write your code no
matter whether you plan to post it or not.
And obviously,
thanx for your solution. I am reviewing it, I was also trying out some
solutions.

You misunderstood. I did not modify your code other than changing the
variable names. My hope was that with this modification any errors sprang
to you eye...

Peter
 
S

SUBHABRATA

Hi Peter,
Peter said:
No, you should use meaningful variable names when you write your code no
matter whether you plan to post it or not.
Good You are teaching me something good in life. Thanx.
You misunderstood. I did not modify your code other than changing the
variable names. My hope was that with this modification any errors sprang
to you eye...
I was seeing that.
I am almost near the solution. You can also try some hands if you
feel.
Best Regards,
Subhabrata.
 
P

ptn

Good You are teaching me something good in life. Thanx.



I was seeing that.
I am almost near the solution. You can also try some hands if you
feel.
Best Regards,
Subhabrata.

A couple more things on variable naming and coding style:

- You used "a{digit}" to name variables of different types (a4 was an
int, a2 was a list and the rest were strings). Remember C, where i, j,
k are indices, p, q, r are pointers, s, t are strings and x, y, z are
integers. For unimportant variables, you can skip long descriptive
names, so long you don't use a confusing one.

- You violated your own naming conventions. Why did you choose to use
s to name that last string? Use descriptive names and stick to your
own style.

- You use whitespace weirdly (like in a4>-1 or a4=a3.find).

Try reading PEP8 (http://www.python.org/dev/peps/pep-0008/), the Style
Guide for Python Code.

As for your code, you need to find where it is that missing_word and
first_char are being updated, and assign to s before that happens.
 
T

Terry Reedy

SUBHABRATA said:
Sorry if I didn't say that.
The input is a string "Petrol Helium Heaven Sky"
Now, in a3 it is "God Goddess Heaven Sky" is there,
> ...I was looking for an output of "H S Petrol Helium"

Meaningful names, splitting the target string, and using 'in' makes the
code much easier.

inwords = "Petrol Helium Heaven Sky".split()
targets = "God Goddess Heaven Sky".split()
found = []
not_found = []

for word in inwords:
if word in targets:
found.append(word[0])
else:
not_found.append(word)

found.extend(not_found)
print(' '.join(found)) # 3.0

#prints the requested
H S Petrol Helium
 
J

John Machin

Remember C, where i, j,
k are indices, p, q, r are pointers, s, t are strings and x, y, z are
integers.

Only by convention (even-K&R-v1 C required explicit declarations
almost everywhere), and x etc being used for integers is news to
me ... perhaps you were thinking of m and n.

The only language I remember that had implicit typing was FORTRAN (GOD
is real, but JESUS is an integer).
 
P

ptn

Only by convention (even-K&R-v1 C required explicit declarations
almost everywhere), and x etc being used for integers is news to
me ... perhaps you were thinking of m and n.

The only language I remember that had implicit typing was FORTRAN (GOD
is real, but JESUS is an integer).

Yes, I meant by convention.

x is the first name that comes to mind when declaring an unimportant
int. Perhaps it's just me.
 
M

MRAB

Hadn't seen that one before -- maybe because I learned FORTRAN IV at a
college in the middle of west Michigan; I think half the population was
one step away from turning Amish <G>

I learned it by the word: Indian -- implicit integers began at I, and
ended with N.
You could've just used the first 2 letters of 'integer'! :)
 
S

SUBHABRATA

Thanx Terry it worked, I was thinking if input_string could be
searched from given_string and replaced in the input_string but your
one is smarter.
And Peter, thanx for trying to teach conventions. But if I write
explicit comments, and carry on using my own variables( I can use
anything as they are variables:) ) according to my own ease isn't
that good?
Best Regards,
Subhabrata.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top