hi all
am searching for a key in a list, am using
Found = 0
for item in list:
if not key == item:
Found = 0
elif key == item:
Found =1
Now based on the Found value i ll manipulate the list.
but whenever the "key and item" doesnt match it makes "Found = 0", even
though i have an item that matches the key, but it overwrites the Found
variable to 0, in the next loop when "key ! = item", and hence all my
calculations are going wrong,
If i find a key in my list, some how the variable "Found " should be
1,till it comes out of the loop.
How can i do that.
thanks in advance
yogi 4 1045 mu*******@yahoo.com wrote: hi all am searching for a key in a list, am using
Found = 0 for item in list: if not key == item: Found = 0 elif key == item: Found =1
Now based on the Found value i ll manipulate the list. but whenever the "key and item" doesnt match it makes "Found = 0", even though i have an item that matches the key, but it overwrites the Found variable to 0, in the next loop when "key ! = item", and hence all my calculations are going wrong, If i find a key in my list, some how the variable "Found " should be 1,till it comes out of the loop.
How can i do that.
Well, one way would be:
Found = False
for item in list:
if key == item:
Found = True
break
or maybe:
for item in list:
if key == item:
Found = True
break
else:
Found = False
but a better way would be:
Found = (key in list)
-tim thanks in advance yogi
hi tim
thanks for the help
acutally i dint tell u the whole problem
i have list of lists, for ex
list1 =[ ['a','b','c','d','e'] , ['1','2','3','4'] , ['A','B','C','D']
]
and another list
list2 = ['1','2','5',4]
now am searching the items of list2 in list1 as below
Found = True
for i in list1:
for j in i:
if not list2[1] == j or not list2[2] == j:
if not list2[0] == j:
Found = False
elif list2[0] == j:
Found = True
break
now though it finds the the key in between and sets the "Found=True",
but later in the next list of if one item doesnt match it sets again
Found = False
i hope am clear this time
how to keep the Found = True for always if at all i find the item even
once.
thanks again in advance
yogi mu*******@yahoo.com writes: hi tim thanks for the help acutally i dint tell u the whole problem i have list of lists, for ex
list1 =[ ['a','b','c','d','e'] , ['1','2','3','4'] , ['A','B','C','D'] ]
and another list
list2 = ['1','2','5',4]
now am searching the items of list2 in list1 as below
Found = True for i in list1: for j in i: if not list2[1] == j or not list2[2] == j: if not list2[0] == j: Found = False elif list2[0] == j: Found = True break
now though it finds the the key in between and sets the "Found=True", but later in the next list of if one item doesnt match it sets again Found = False
i hope am clear this time
You have half the solution. Basically, you should set Found to one
state, and then only set it againn if it changes. For instance:
Found = True
for i in list1:
for j in i:
if not list2[1] == j or not list2[2] == j:
if not list2[0] == j:
Found = False
This will exit the outer loop with Found being False if and only if
the nested if's in your inner loop set it to False; you never need to
set it to True. Alternatively, set the initial state to False, and
only set it to True if you find something.
For efficiency, you can break from the inner loop, and then check
found after the inner loop and break a second time if it's changed.
BTW, the nested ifs in your inner loop don't do what your description
says they should.
<mike
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
I'm having trouble determining what you want but
I think there are a couple of problems in your
code:
list2 = ['1','2','5',4]
did you mean
list2 = ['1','2','3','4']
Note missing quotes around the 4 and
5 instead of 3
If you want to know if list2 is found in list 1
it is as simple as:
if list2 in list1:
#
# Do something here
#
if you want to know if any member of list2 is
found in any list in list 1 then you can just do:
Found=max([y in x for y in list2 for x in list1])
works and you should have some fun picking this one-liner apart
<grin>.
-Larry Bates
Mike Meyer wrote: mu*******@yahoo.com writes:
hi tim thanks for the help acutally i dint tell u the whole problem i have list of lists, for ex
list1 =[ ['a','b','c','d','e'] , ['1','2','3','4'] , ['A','B','C','D'] ]
and another list
list2 = ['1','2','5',4]
now am searching the items of list2 in list1 as below
Found = True for i in list1: for j in i: if not list2[1] == j or not list2[2] == j: if not list2[0] == j: Found = False elif list2[0] == j: Found = True break
now though it finds the the key in between and sets the "Found=True", but later in the next list of if one item doesnt match it sets again Found = False
i hope am clear this time
You have half the solution. Basically, you should set Found to one state, and then only set it againn if it changes. For instance:
Found = True for i in list1: for j in i: if not list2[1] == j or not list2[2] == j: if not list2[0] == j: Found = False
This will exit the outer loop with Found being False if and only if the nested if's in your inner loop set it to False; you never need to set it to True. Alternatively, set the initial state to False, and only set it to True if you find something.
For efficiency, you can break from the inner loop, and then check found after the inner loop and break a second time if it's changed.
BTW, the nested ifs in your inner loop don't do what your description says they should.
<mike
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/ Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Santanu Chatterjee |
last post by:
Hello everybody,
I am very new to python. I have a query about
list in python.
Suppose I have a list
a = ,5,6,7,,11,12]
I want to know if there is any simple python
facility available that...
|
by: krystoffff |
last post by:
Hi
I would like to paginate the results of a query on several pages. So I
use a query with a limit X offset Y to display X results on a page,
ok.
But for the first page, I need to run the...
|
by: Bernard Lebel |
last post by:
Hello,
I'm stumbled at a serious problem, and quite frankly getting
desparate. This is a rather long-winded one so I'll try to get
straight to the point.
I have this Python program, that...
|
by: Caleb Hattingh |
last post by:
Hi everyone
I suspect this has come up before, but google and group searches for
"python package index query" or "pypi query" and the like haven't
turned anything up.
I want to monitor the...
|
by: buterfly0707 |
last post by:
hi..
i want to kow wether can we give a range in sql.
maens i want to get the values in field where one period to another period.
example:ther is two fields in the table period and data. i want...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
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...
|
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...
| |