469,330 Members | 1,257 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,330 developers. It's quick & easy.

Combining two lists [python union operators]

Hi!

I have a Python problem which is my last problem to solve to finish up a Django application. This is amazingly simple but I have been stuck now for a couple of days. It is embarrisingly simple. . . but not simple for me.

I have two lists. I end up creating the two lists, with the end result being that each gets created at a different indent level:
Expand|Select|Wrap|Line Numbers
  1.   mylist = entry.objects.filter(title__contains=searchterms1) | entry.objects.filter(keywords__contains=searchterms1).order_by('-dtime')
  2.  
  3.   if (andor2=="1") & (NOT2=="0") :
  4.       mylist3 = mylist.filter(title__contains=searchterms3) & mylist.filter(keywords__contains=searchterms3).order_by('-dtime')
  5.       mylistWHAT=mylist3
  6.  
  7. # here is the big problem and it is a Pythonic problem
  8. #  I need to somehow union the two lists mylistWHAT and mylistWHOM, but they are at different indents.  
  9.  
  10.   mylist=(mylistWHOM  | mylistWHAT)
  11.  
So my problem might be solved by defining both lists as global (which I keep getting syntax errors for - so I don't know how to do this), but it may be that there is some other way to get this last statement to work. I've tried various indents, but this is the only one that the interpreter likes - but one of the lists is then empty.

So how can I declare them global and how can I combine them?

Thanks so much for taking any time to look at this!

-Nat
Jul 7 '07 #1
5 44077
bartonc
6,596 Expert 4TB
Hi!

I have a Python problem which is my last problem to solve to finish up a Django application. This is amazingly simple but I have been stuck now for a couple of days. It is embarrisingly simple. . . but not simple for me.

I have two lists. I end up creating the two lists, with the end result being that each gets created at a different indent level:
Expand|Select|Wrap|Line Numbers
  1.   mylist = entry.objects.filter(title__contains=searchterms1) | entry.objects.filter(keywords__contains=searchterms1).order_by('-dtime')
  2.  
  3.   if (andor2=="1") & (NOT2=="0") :
  4.       mylist3 = mylist.filter(title__contains=searchterms3) & mylist.filter(keywords__contains=searchterms3).order_by('-dtime')
  5.       mylistWHAT=mylist3
  6.  
  7. # here is the big problem and it is a Pythonic problem
  8. #  I need to somehow union the two lists mylistWHAT and mylistWHOM, but they are at different indents.  
  9.  
  10.   mylist=(mylistWHOM  | mylistWHAT)
  11.  
So my problem might be solved by defining both lists as global (which I keep getting syntax errors for - so I don't know how to do this), but it may be that there is some other way to get this last statement to work. I've tried various indents, but this is the only one that the interpreter likes - but one of the lists is then empty.

So how can I declare them global and how can I combine them?

Thanks so much for taking any time to look at this!

-Nat
Is this what you are trying to do:
Expand|Select|Wrap|Line Numbers
  1. >>> list1 = [1, 2, 3]
  2. >>> list2 = [4, 5, 6]
  3. >>> list1 + list2
  4. [1, 2, 3, 4, 5, 6]
  5. >>> 
?
Jul 7 '07 #2
bartonc
6,596 Expert 4TB
Is this what you are trying to do:
Expand|Select|Wrap|Line Numbers
  1. >>> list1 = [1, 2, 3]
  2. >>> list2 = [4, 5, 6]
  3. >>> list1 + list2
  4. [1, 2, 3, 4, 5, 6]
  5. >>> 
?
Or, perhaps:
Expand|Select|Wrap|Line Numbers
  1. >>>[list1, list2]
  2. [[1, 2, 3], [4, 5, 6]]
  3. >>> 
or
Expand|Select|Wrap|Line Numbers
  1. >>> zip(list1, list2)
  2. [(1, 4), (2, 5), (3, 6)]
  3. >>> 
The indentation doesn't matter. They are in the same scope (the function/method scope) so they are both known to the assignment that you are using. The big thing is "what default value to use when the IF is not taken".
Jul 7 '07 #3
bartonc
6,596 Expert 4TB
Or, perhaps:
Expand|Select|Wrap|Line Numbers
  1. >>>[list1, list2]
  2. [[1, 2, 3], [4, 5, 6]]
  3. >>> 
or
Expand|Select|Wrap|Line Numbers
  1. >>> zip(list1, list2)
  2. [(1, 4), (2, 5), (3, 6)]
  3. >>> 
The indentation doesn't matter. They are in the same scope (the function/method scope) so they are both known to the assignment that you are using. The big thing is "what default value to use when the IF is not taken".
Depending on your version of python (you may need to import the set module in older versions), you can create a true union like this:
Expand|Select|Wrap|Line Numbers
  1. >>> list1 = [1, 2, 3, 2]
  2. >>> list2 = [4, 5, 6, 4]
  3. >>> set.union(set(list1), set(list2))
  4. set([1, 2, 3, 4, 5, 6])
  5. >>> 
Jul 7 '07 #4
Depending on your version of python (you may need to import the set module in older versions), you can create a true union like this:
Expand|Select|Wrap|Line Numbers
  1. >>> list1 = [1, 2, 3, 2]
  2. >>> list2 = [4, 5, 6, 4]
  3. >>> set.union(set(list1), set(list2))
  4. set([1, 2, 3, 4, 5, 6])
  5. >>> 

Thanks heaps! Your info really helps. I now know that my indentation is not the issue, and I have a nice way to set unions!

Have a super weekend!

-Warren
Jul 7 '07 #5
bartonc
6,596 Expert 4TB
Thanks heaps! Your info really helps. I now know that my indentation is not the issue, and I have a nice way to set unions!

Have a super weekend!

-Warren
It's my pleasure. Any time, really.

You have a great weekend, as well (I'll be toiling away),
Barton
Jul 7 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

226 posts views Thread by Stephen C. Waterbury | last post: by
42 posts views Thread by Jeff Wagner | last post: by
2 posts views Thread by Michael Foord | last post: by
5 posts views Thread by Andy Kent | last post: by
14 posts views Thread by AliceB.Toklas | last post: by
reply views Thread by dalebryan1 | last post: by
4 posts views Thread by antar2 | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
reply views Thread by haryvincent176 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.