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: -
mylist = entry.objects.filter(title__contains=searchterms1) | entry.objects.filter(keywords__contains=searchterms1).order_by('-dtime')
-
-
if (andor2=="1") & (NOT2=="0") :
-
mylist3 = mylist.filter(title__contains=searchterms3) & mylist.filter(keywords__contains=searchterms3).order_by('-dtime')
-
mylistWHAT=mylist3
-
-
# here is the big problem and it is a Pythonic problem
-
# I need to somehow union the two lists mylistWHAT and mylistWHOM, but they are at different indents.
-
-
mylist=(mylistWHOM | mylistWHAT)
-
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
5 44247
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: -
mylist = entry.objects.filter(title__contains=searchterms1) | entry.objects.filter(keywords__contains=searchterms1).order_by('-dtime')
-
-
if (andor2=="1") & (NOT2=="0") :
-
mylist3 = mylist.filter(title__contains=searchterms3) & mylist.filter(keywords__contains=searchterms3).order_by('-dtime')
-
mylistWHAT=mylist3
-
-
# here is the big problem and it is a Pythonic problem
-
# I need to somehow union the two lists mylistWHAT and mylistWHOM, but they are at different indents.
-
-
mylist=(mylistWHOM | mylistWHAT)
-
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: -
>>> list1 = [1, 2, 3]
-
>>> list2 = [4, 5, 6]
-
>>> list1 + list2
-
[1, 2, 3, 4, 5, 6]
-
>>>
?
Is this what you are trying to do: -
>>> list1 = [1, 2, 3]
-
>>> list2 = [4, 5, 6]
-
>>> list1 + list2
-
[1, 2, 3, 4, 5, 6]
-
>>>
?
Or, perhaps: -
>>>[list1, list2]
-
[[1, 2, 3], [4, 5, 6]]
-
>>>
or -
>>> zip(list1, list2)
-
[(1, 4), (2, 5), (3, 6)]
-
>>>
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".
Or, perhaps: -
>>>[list1, list2]
-
[[1, 2, 3], [4, 5, 6]]
-
>>>
or -
>>> zip(list1, list2)
-
[(1, 4), (2, 5), (3, 6)]
-
>>>
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: -
>>> list1 = [1, 2, 3, 2]
-
>>> list2 = [4, 5, 6, 4]
-
>>> set.union(set(list1), set(list2))
-
set([1, 2, 3, 4, 5, 6])
-
>>>
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: -
>>> list1 = [1, 2, 3, 2]
-
>>> list2 = [4, 5, 6, 4]
-
>>> set.union(set(list1), set(list2))
-
set([1, 2, 3, 4, 5, 6])
-
>>>
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
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
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
3 posts
views
Thread by Mickel Grönroos |
last post: by
|
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
| | | | | | | | | | |