0

I'm Trying to figure out how this works. Example One:

n = ["1ab", "2an", "3bca", "4adc"]
l = ["1", "2", "3"]
for m in n:
 if "a" in m:
 for k in l:
 if k in m:
 print k
1
2
3

Now I will try to print last member of n list.

n = ["1ab", "2an", "3bca", "4adc"]
l = ["1", "2", "3"]
for m in n:
 if "a" in m:
 for k in l:
 if not k in m:
 print k
2
3
1
3
1
2
1
2
3

I need to print a list member which does not contain any number listed in l variable but contains "a" in it.

asked Nov 5, 2013 at 18:42
3
  • 2
    Please explain a bit more what you are trying to do Commented Nov 5, 2013 at 18:44
  • I need to print a list member which does not contain any number listed in l variable but contains "a" in it. Commented Nov 5, 2013 at 18:52
  • Then you should edit your question Commented Nov 5, 2013 at 19:11

4 Answers 4

7

Since 4 is not in your list l , you cannot print it.

bukzor
38.7k13 gold badges84 silver badges116 bronze badges
answered Nov 5, 2013 at 18:44

3 Comments

Thanks for reply. So how to print it?
@iRex it helps a bit more if you say what your are trying to achieve?
@ Srinivas Reddy Thatiparthy I need to compare if variable n contains any number of indicated in the variable l. Then Print them separately. In second example I need to print ""4adc".
2
n = ["1ab", "2an", "3bca", "4adc"]
l = ["1", "2", "3"]
for m in n:
 if "a" in m:
 if not any([k in m for k in l]):
 print m
4adc
answered Nov 5, 2013 at 19:08

Comments

1

The answer is in your second to last line. For every loop through n you loop through l and there are three members of n that meet if not k in m: condition. So

Loop 1 prints: 2,3 Loop 2 prints: 1,3 Loop 3 prints: 1,2 Loop 4 prints: 1,2,3

answered Nov 5, 2013 at 19:00

Comments

1
>>> l 
['1', '2', '3']
>>> n 
['1ab', '2an', '3bca', '4adc']
for el in n: 
 if(el[0] not in l):
 print(el)
4adc

Or if you just want to print 4, based on your list sequence:

for el in n: 
 if(el[0] not in l):
 print(el[0])

Now you just added to your question, "but contains "a"", add second iff.

 for el in n: 
 if(el[0] not in l):
 if('a' in el):
 print(el[0],el)
answered Nov 5, 2013 at 19:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.