Electronics & Programming

develissimo

Open Source electronics development and programming

  • You are not logged in.

#1 Dec. 9, 2010 10:34:42

Phani C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Help for for loop


i am passing though a list{1,2,3,4} but i want only the numbers with
odd index in template written in html

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#2 Dec. 9, 2010 10:44:31

Cal L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Help for for loop


>>> mylist =
>>> filter(lambda x: x%2, mylist)

>>>

This what you need?

On 09/12/2010 10:34, Phani Chand wrote:i am passing though a list{1,2,3,4} but i want only the numbers withodd index in template written in html --You received this message because you are subscribed to the GoogleGroups "Django users" group.To post to this group, send email to django-us...@googlegroups.com.To unsubscribe from this group, send email todjango-users+unsubscr...@googlegroups.com.For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#3 Dec. 9, 2010 11:18:47

Phani C.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Help for for loop


Can i use filter(lambda x: x%2, mylist) directly in my html page????

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#4 Dec. 9, 2010 11:24:31

Cal L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Help for for loop


Uh, you *might* be able to use:

{% for x in mylist %}
{% if x % 2 %}
yay: {{x}}
{% else %}
nay: {{x}}
{% endif %}
{% endfor %}

On 09/12/2010 11:18, Phani Chand wrote:Can i use filter(lambda x: x%2, mylist) directly in my html page???? --You received this message because you are subscribed to the GoogleGroups "Django users" group.To post to this group, send email to django-us...@googlegroups.com.To unsubscribe from this group, send email todjango-users+unsubscr...@googlegroups.com.For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#5 Dec. 9, 2010 11:25:05

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Help for for loop


Hmm, those are the values that are odd, he wanted the values from odd
indices, eg:

>>> a=
>>> filter(lambda x: x%2, a)

>>>


That could probably be written a bit nicer..

On Thu, Dec 9, 2010 at 10:44 AM, Cal Leeming
<cal.leem...@simplicitymedialtd.co.uk> wrote:
>>>> mylist =
>>>> filter(lambda x: x%2, mylist)
>
>>>>
>
> This what you need?
>
> On 09/12/2010 10:34, Phani Chand wrote:
>>
>> i am passing though a list{1,2,3,4} but i want only the numbers with odd
>> index in template written in html --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to django-us...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> django-users+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>>http://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups
> "Django users" group.
> To post to this group, send email to django-us...@googlegroups.com.
> To unsubscribe from this group, send email to
> django-users+unsubscr...@googlegroups.com.
> For more options, visit this group at
>http://groups.google.com/group/django-users?hl=en.
>
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#6 Dec. 9, 2010 11:27:21

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Help for for loop


On 9 déc, 12:18, Phani Chand <balaphanich...@gmail.com> wrote:
> Can i use filter(lambda x: x%2, mylist) directly in my html page????

s/html page/template/

And no, you cannot use Python code in a template.
You can either
1/ filter the list in the view
2/ use the builtin "divisibleby" filter (http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#divisibleby)
3/ write your own "odds" and "evens" filters (http://
docs.djangoproject.com/en/1.2/howto/custom-template-tags/)

HTH

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#7 Dec. 9, 2010 11:28:15

Cal L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Help for for loop


I see no need to get itertools involved ;) Why not just use this:

filter(lambda x: not x%2, a)


On 09/12/2010 11:24, Tom Evans wrote:Hmm, those are the values that are odd, he wanted the values from odd
indices, eg:a=
filter(lambda x: x%2, a)

That could probably be written a bit nicer..

On Thu, Dec 9, 2010 at 10:44 AM, Cal Leeming
<cal.leem...@simplicitymedialtd.co.uk> wrote:mylist =
filter(lambda x: x%2, mylist)
This what you need?

On 09/12/2010 10:34, Phani Chand wrote:i am passing though a list{1,2,3,4} but i want only the numbers with odd
index in template written in html --
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#8 Dec. 9, 2010 11:29:49

Cal L.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Help for for loop


Can't you use the % operator within if statements in templates? I wasalmost sure I'd done this before :|On 09/12/2010 11:27, bruno desthuilliers wrote:On 9 déc, 12:18, Phani Chand<balaphanich...@gmail.com> wrote:Can i use filter(lambda x: x%2, mylist) directly in my html page????s/html page/template/

And no, you cannot use Python code in a template.
You can either
1/ filter the list in the view
2/ use the builtin "divisibleby" filter (http://docs.djangoproject.com/en/1.2/ref/templates/builtins/#divisibleby)
3/ write your own "odds" and "evens" filters (http://
docs.djangoproject.com/en/1.2/howto/custom-template-tags/)

HTH--
You received this message because you are subscribed to the Google Groups "Django
users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#9 Dec. 9, 2010 11:36:33

Tom E.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Help for for loop


Because that looks at the values of the array and returns the items
which have a value that is divisible by two, not the position of the
value within the array. It is simpler to comprehend if you don't put
numbers in the list:

>>> l =
>>> filter(lambda x: not x%2, l)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 1, in <lambda>
TypeError: not all arguments converted during string formatting
>>>


What the OP originally asked for was for items in the i'th position in
a list, where i is odd. Your solutions all ignore the position, and
only look at the value.

On Thu, Dec 9, 2010 at 11:27 AM, Cal Leeming
<cal.leem...@simplicitymedialtd.co.uk> wrote:
> I see no need to get itertools involved ;) Why not just use this:
>
> filter(lambda x: not x%2, a)
>
>
> On 09/12/2010 11:24, Tom Evans wrote:
>>
>> Hmm, those are the values that are odd, he wanted the values from odd
>> indices, eg:
>>
>>>>> a=
>>>>> filter(lambda x: x%2, a)
>>
>>
>>>>>
>>>>>
>>
>>
>>
>> That could probably be written a bit nicer..
>>
>> On Thu, Dec 9, 2010 at 10:44 AM, Cal Leeming
>> <cal.leem...@simplicitymedialtd.co.uk>  wrote:
>>>>>>
>>>>>> mylist =
>>>>>> filter(lambda x: x%2, mylist)
>>>
>>>
>>> This what you need?
>>>
>>> On 09/12/2010 10:34, Phani Chand wrote:
>>>>
>>>> i am passing though a list{1,2,3,4} but i want only the numbers with odd
>>>> index in template written in html --
>>>> You received this message because you are subscribed to the Google
>>>> Groups
>>>> "Django users" group.
>>>> To post to this group, send email to django-us...@googlegroups.com.
>>>> To unsubscribe from this group, send email to
>>>> django-users+unsubscr...@googlegroups.com.
>>>> For more options, visit this group at
>>>>http://groups.google.com/group/django-users?hl=en.
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Django users" group.
>>> To post to this group, send email to django-us...@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> django-users+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>>http://groups.google.com/group/django-users?hl=en.
>>>
>>>
>

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

#10 Dec. 9, 2010 12:04:02

bruno d.
Registered: 2009-11-02
Reputation: +  0  -
Profile   Send e-mail  

Help for for loop


On 9 déc, 12:24, Tom Evans <tevans...@googlemail.com> wrote:
> Hmm, those are the values that are odd, he wanted the values from odd
> indices

Well spotted ;)

There's a builtin "slice" filter that should have done the trick but I
just couldn't manage to make it work with a for loop :-/

The only solution I could come with that does not require custom
filters or whatever is using the cycle tag (http://
docs.djangoproject.com/en/1.2/ref/templates/builtins/#cycle):

{% for item in list %}
{% cycle "even", "odd" as oddeven %} {# assuming standard 0-based
indexing #}
{% if oddeven == "odd" %}
<p>item {{ item }} is at odd index {{ forloop.counter }}</p>
{% endif %}
{% endfor %}


NB : not tested...

--
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to django-us...@googlegroups.com.
To unsubscribe from this group, send email to
django-users+unsubscr...@googlegroups.com.
For more options, visit this group athttp://groups.google.com/group/django-users?hl=en.

Offline

Board footer

Moderator control

Enjoy the 21st of October
PoweredBy

The Forums are managed by develissimo stuff members, if you find any issues or misplaced content please help us to fix it. Thank you! Tell us via Contact Options
Leave a Message
Welcome to Develissimo Live Support