|
P: 2
|
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
| |
Share this Question
| Expert 5K+
P: 5,734
|
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]
-
>>>
?
| | | Expert 5K+
P: 5,734
|
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".
| | | Expert 5K+
P: 5,734
|
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])
-
>>>
| | |
P: 2
|
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
| | | Expert 5K+
P: 5,734
|
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 Help answer this question
Didn't find the answer to your Python question?
| | Question stats - viewed: 31510
- replies: 5
- date asked: Jul 7 '07
|