By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,282 Members | 1,209 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,282 IT Pros & Developers. It's quick & easy.

Help me!!

P: n/a
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[b]:
files.append(ab_file[b].upper())
else:
files.append(ab_file[b])
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]

Aug 16 '07 #1
Share this Question
Share on Google+
3 Replies

P: n/a
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
Aug 16 '07 #2

P: n/a
On Aug 16, 2:26 pm, beginner <zyzhu2...@gmail.comwrote:
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 -

Aug 16 '07 #3

P: n/a
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]
Aug 16 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.