Help me!!

R

Rohan

I have the following piece of code

a = len(ab_file)
b= 0
while(b<a):
c= 0
d = len(cd_files)
while(c<d):
if cd_files[c] == ab_file:
files.append(ab_file.upper())
else:
files.append(ab_file)
c = c + 1

b = b +1

print files

I'm trying to compare the list with another list and if it is there in
both I'm changing it to upper case and adding to another list and if
its not there in both I just want to add it to the global list (ie
files).
I'm able to do the first part but when I'm doing the second part the
files which are not there in both are getting repeatedly into the
global file. Some one help me so that
If ab = [a,b,c,d]
and cd = [a,c]
my global list file should be [A,b,C,d]
 
B

beginner

Some one help me so that
If ab = [a,b,c,d]
and cd = [a,c]
my global list file should be [A,b,C,d]


If there is a lot of entries in the list, I would consider using an
indexed data structure such as dict.
ab=['a','b','c','d']
cd=['a','c']
common=[x.upper() for x in ab if x in cd]
ab_minus_cd=[x for x in ab if x not in cd]
cd_minus_ab=[x for x in ab if x not in cd]
requested_list = common+ab_minus_cd+cd_minus_ab
 
B

beginner

Some one help me so that
If ab = [a,b,c,d]
and cd = [a,c]
my global list file should be [A,b,C,d]

If there is a lot of entries in the list, I would consider using an
indexed data structure such as dict.


ab=['a','b','c','d']
cd=['a','c']
common=[x.upper() for x in ab if x in cd]
ab_minus_cd=[x for x in ab if x not in cd]
cd_minus_ab=[x for x in ab if x not in cd]
requested_list = common+ab_minus_cd+cd_minus_ab- Hide quoted text -
 
G

Gerardo Herzig

Anhoter HURRA for list comprehensions:
>>> a = ['a', 'b', 'c', 'd']
>>> b = ['a','c']
>>> [x in b and x.upper() or x for x in a]
['A', 'b', 'C', 'd']

Is what you want?
Cheers

Gerardo
I'm trying to compare the list with another list and if it is there in
both I'm changing it to upper case and adding to another list and if
its not there in both I just want to add it to the global list (ie
files).
I'm able to do the first part but when I'm doing the second part the
files which are not there in both are getting repeatedly into the
global file. Some one help me so that
If ab = [a,b,c,d]
and cd = [a,c]
my global list file should be [A,b,C,d]
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top