473,795 Members | 2,919 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question on Joining of list

Dear Group,
I am trying the following code line:
def try2(n):
a1=raw_input("P RINT 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.
Jul 18 '08 #1
13 1008
On Fri, 18 Jul 2008 01:31:59 -0700, SUBHABRATA wrote:
def try2(n):
a1=raw_input("P RINT 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
Jul 18 '08 #2
SUBHABRATA wrote:
I am trying the following code line:
def try2(n):
user_line = raw_input("PRIN T A STRING:")
user_words = user_line.split ()
my_line = "God Godess Heaven Sky"
for word in user_words:
pos = my_line.find(wo rd)
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
Jul 18 '08 #3
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..

Marc 'BlackJack' Rintsch wrote:
On Fri, 18 Jul 2008 01:31:59 -0700, SUBHABRATA wrote:
def try2(n):
a1=raw_input("P RINT 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
Jul 18 '08 #4
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 Otten wrote:
SUBHABRATA wrote:
I am trying the following code line:

def try2(n):
user_line = raw_input("PRIN T A STRING:")
user_words = user_line.split ()
my_line = "God Godess Heaven Sky"
for word in user_words:
pos = my_line.find(wo rd)
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
Jul 18 '08 #5
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.

SUBHABRATA wrote:
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..

Marc 'BlackJack' Rintsch wrote:
On Fri, 18 Jul 2008 01:31:59 -0700, SUBHABRATA wrote:
def try2(n):
a1=raw_input("P RINT 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
Jul 18 '08 #6
SUBHABRATA wrote:
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
Jul 18 '08 #7
Hi Peter,
Peter Otten wrote:
SUBHABRATA wrote:
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.
Good You are teaching me something good in life. Thanx.
>
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...
I was seeing that.
I am almost near the solution. You can also try some hands if you
feel.
Best Regards,
Subhabrata.
>
Peter
Jul 18 '08 #8
ptn
On Jul 18, 5:40*am, SUBHABRATA <subhabrata.i.. .@hotmail.comwr ote:
Hi Peter,Peter Otten wrote:
SUBHABRATA wrote:
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.

Good You are teaching me something good in life. Thanx.
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...

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

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.
Jul 18 '08 #9


SUBHABRATA wrote:
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(wo rd[0])
else:
not_found.appen d(word)

found.extend(no t_found)
print(' '.join(found)) # 3.0

#prints the requested
H S Petrol Helium

Jul 18 '08 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
1954
by: Carlos Ribeiro | last post by:
As a side track of my latest investigations, I began to rely heavily on generators for some stuff where I would previsouly use a more conventional approach. Whenever I need to process a list, I'm tending towards the use of generators. One good example is if I want to print a report, or to work over a list with complex processing for each item. In both cases, a simple list comprehension can't be used. The conventional approach involves...
3
583
by: Alex | last post by:
Hi, I need to form a query where i can add some columns based on the result. Table A ColA, ColB ---------- 1 A 2 B
10
2625
by: Paulo Jan | last post by:
Hi all: Let's say I'm designing a database (Postgres 7.3) with a list of all email accounts in a certain server: CREATE TABLE emails ( clienteid INT4, direccion VARCHAR(512) PRIMARY KEY, login varchar(128) NOT NULL,
9
2003
by: Riley DeWiley | last post by:
I have a programming problem in OLEDB and C++ that seems to be pointing me toward using layered views and hierarchical rowsets. However, I am uncertain of the precise implementation and need guidance from someone experienced in those areas. The problem, in the abstract, is how to select a set of records in one table, each one of which has a relation to every one of a set of records in another table, the tables being joined in a...
11
2291
by: Madison Kelly | last post by:
Hi all, I am new to the list and I didn't want to seem rude at all so I wanted to ask if this was okay first. I have a program I have written in perl which uses a postgresSQL database as the backend. The program works but the performance is really bad. I have been reading as much as I can on optimizing performance but still it isn't very reasonable. At one point I had my program able to process 175,000 records in 16min 10sec on a...
2
1128
by: Howard | last post by:
I need to write a web page that outputs a table from database. ( i want to write my own code, don't want to use the built-in control) I came up with two ways of doing this (i use c# 2.0) 1. pesudo code sqlstringTable = "select category_name from table1" execute(sqlstringTable) while(read)
1
2892
by: lee | last post by:
Hi This is a dumb question as I know it's fairly easy but I cant seem to find an example after two hours of searching. In a stored procedure I'm trying to build up a string eg, (the following is in a loop) IF @xcount 0
25
4204
by: Nicholas Parsons | last post by:
Howdy Folks, I was just playing around in IDLE at the interactive prompt and typed in dir({}) for the fun of it. I was quite surprised to see a pop method defined there. I mean is that a misnomer or what? From the literature, pop is supposed to be an operation defined for a stack data structure. A stack is defined to be an "ordered" list data structure. Dictionaries in Python have no order but are sequences. Now, does anyone know...
1
1390
by: jehugaleahsa | last post by:
I have some code that looks like this: int customerIds = { 1, 2, 3, 4, 5, 6, 7, 8 }; List<Threadthreads = new List<Threads>(); foreach (int customerId in customerIds) { Thread thread = new Thread(delegate() { // do calculation with customerId });
13
1487
by: Thomas Troeger | last post by:
Hi, Sorry I've posted a similar question some weeks ago, but I got no answers. I want to embed a Python application on a device with limited resources, esp. storage limitations. Is there a way to reduce the Python interpreter to a set of modules that's urgently needed? Or is there a method to have gzipped modules that are unzipped on the fly into memory when they're accessed? That would be even better. Additionally, is there a Python...
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10216
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10165
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10002
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9044
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7543
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5565
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.