472,127 Members | 1,620 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,127 software developers and data experts.

Help me!!

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
3 950
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
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
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.

Similar topics

3 posts views Thread by Krakatioison | last post: by
3 posts views Thread by Lodewijk van Haringhal | last post: by
6 posts views Thread by wukexin | last post: by
1 post views Thread by Rahul | last post: by
7 posts views Thread by Mickyd1561 | last post: by
reply views Thread by debipraad | last post: by
22 posts views Thread by Amali | last post: by
3 posts views Thread by RedRoses | last post: by
5 posts views Thread by tabani | last post: by
reply views Thread by leo001 | last post: by

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.