help creating a new list based on two lists values?

This is the place for queries that don't fit in any of the other categories.

help creating a new list based on two lists values?

Postby eedodge3 » Tue Jul 30, 2013 11:58 pm

coding is getting easier since i joined this forum. many thanks to the experts. below is my new question regarding list.

compare these lists:
Code: Select all
mylist1=['blue,aaa,bbb,ccc', 'red,xxx,mmm,ooo', 'yellow,ddd,eee,fff','green,ggg,hhh,iii']
mylist2=['yellow,111', 'red,222','brown,333', 'pink,444','blue,555']

desired list:
Code: Select all
newlist=['blue,aaa,bbb,ccc,555','red,xxx,mmm,ooo,222','yellow,ddd,eee,fff,111','green,ggg,hhh,iii,000']


note:
green is not in mylist2.

thanks again.
Last edited by Yoriz on Wed Jul 31, 2013 5:50 am, edited 3 times in total.
Reason: Locked OP. - Added code tags and changed title
eedodge3
 
Posts: 17
Joined: Mon Jul 29, 2013 5:59 pm

Re: help comparing 2 lists

Postby micseydel » Wed Jul 31, 2013 12:19 am

Could you describe this in English? What attempt have you made yourself to solve this problem?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: help comparing 2 lists

Postby eedodge3 » Wed Jul 31, 2013 12:34 am

i want to compare the first item (eg. blue) from mylist1 to mylist2. if 'blue' exist in mylist2, add the value (555) to the blue item in mylist1. or just create a new list with the matched value. if not found (eg. green), put '000'.
eedodge3
 
Posts: 17
Joined: Mon Jul 29, 2013 5:59 pm

Re: help comparing 2 lists

Postby micseydel » Wed Jul 31, 2013 12:50 am

micseydel wrote:What attempt have you made yourself to solve this problem?
Join the #python-forum IRC channel on irc.freenode.net!

Please do not PM members regarding questions which are meant to be discussed publicly. The point of the forum is so that others can benefit from it. We don't want to help you over PMs or emails.
User avatar
micseydel
 
Posts: 1497
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: help creating a new list based on two lists values?

Postby eedodge3 » Wed Jul 31, 2013 8:43 pm

i figured it out. i end up converting my lists to nested lists. for those interested here's how my codes came out. i welcome any suggestions to improve this.

Code: Select all
mylist1=[['blue','aaa','bbb','ccc'],['red','xxx','mmm','ooo'],['yellow','ddd','eee','fff'],['green','ggg','hhh','iii']]
mylist2=[['yellow','111'],['red','222'],['brown','333'],['pink','444'],['blue','555']]

for i,arra in enumerate(mylist1):
    flag=0
    for j,arrb in enumerate(mylist2):
        if arra[0]==arrb[0]:
            arra.append(arrb[1])
            flag=1
            break
    if flag==0:
        arra.append('000')

print mylist1

mylist1=[['blue','aaa','bbb','ccc','555'],['red','xxx','mmm','ooo','222'],['yellow','ddd','eee','fff','111'],['green','ggg','hhh','iii','000']]
eedodge3
 
Posts: 17
Joined: Mon Jul 29, 2013 5:59 pm

Re: help creating a new list based on two lists values?

Postby stranac » Wed Jul 31, 2013 9:21 pm

You don't need enumerate() if you're not going to use indexes.

Also, I would use dicts rather than lists.
That would make the code much simpler.
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1245
Joined: Thu Feb 07, 2013 3:42 pm

Re: help creating a new list based on two lists values?

Postby eedodge3 » Wed Jul 31, 2013 9:59 pm

i looked at the structure of dictionary. it has a key that is unique. in my example, mylist1 the items can have something like this

mylist1=[['blue','aaa','bbb','ccc'], ['blue','aaa','dcs','xyz],['red','xxx','mmm','ooo'],['yellow','ddd','eee','fff'],['green','ggg','hhh','iii']]

some will have the same first item (blue) but rest are different.

if i understood it wrong, please correct me.
eedodge3
 
Posts: 17
Joined: Mon Jul 29, 2013 5:59 pm

Re: help creating a new list based on two lists values?

Postby Kebap » Thu Aug 01, 2013 12:45 pm

As stranac indicated, you can replace the line "for j,arrb in enumerate(mylist2):" with "for arrb in mylist2:"
Learn: How To Ask Questions The Smart Way
Join the #python-forum IRC channel on irc.freenode.net and chat with uns directly!
Kebap
 
Posts: 402
Joined: Thu Apr 04, 2013 1:17 pm
Location: Germany, Europe

Re: help creating a new list based on two lists values?

Postby stranac » Thu Aug 01, 2013 1:20 pm

eedodge3 wrote:if i understood it wrong, please correct me.

No, you got it right.
If that's what you need, you can't use dicts(unless they contain lists of lists).
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1245
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: No registered users and 4 guests