Re: Help for for loop

2010-12-10 Thread Steve Holden
On 12/9/2010 12:24 PM, Cal Leeming [Simplicity Media Ltd] wrote:
> Uh, you *might* be able to use:
> 
> {% for x in mylist %}
> {% if x % 2 %}
> yay: {{x}}
> {% else %}
> nay: {{x}}
> {% endif %}
> {% endfor %}
> 
Blerch! This is a really good indication of why it's much better to
perform the appropriate translations in the view.

regards
 Steve

> 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 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.


-- 
DjangoCon US 2011 Portland, OR: September 6-8 http://djangocon.us/

-- 
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.



Re: Help for for loop

2010-12-09 Thread Tom Evans
Ahh thanks Bruno, that gave the correct solution:

>>> l
['pos 0', 'pos 1', 'pos 2', 'pos 3']
>>> l[1::2]
['pos 1', 'pos 3']

My slice-fu is clearly weak.

Cheers

Tom

On Thu, Dec 9, 2010 at 12:03 PM, bruno desthuilliers
 wrote:
>
>
> On 9 déc, 12:24, Tom Evans  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" %}
>    item {{ item }} is at odd index {{ forloop.counter }}
>  {% 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 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.



Re: Help for for loop

2010-12-09 Thread Phani Chand
Thanx for help.
I solved the problem using divisibleby

-- 
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.



Re: Help for for loop

2010-12-09 Thread bruno desthuilliers


On 9 déc, 12:24, Tom Evans  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" %}
item {{ item }} is at odd index {{ forloop.counter }}
  {% 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 at 
http://groups.google.com/group/django-users?hl=en.



Re: Help for for loop

2010-12-09 Thread Tom Evans
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 = [ 'pos 0', 'pos 1', 'pos 2', 'pos 3' ]
>>> filter(lambda x: not x%2, l)
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 1, in 
TypeError: not all arguments converted during string formatting
>>> [ val for val, i in itertools.izip(l, itertools.count()) if i % 2 ]
['pos 1', 'pos 3']

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 [Simplicity Media Ltd]
 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=[2,3,4,5,6,7]
> filter(lambda x: x%2, a)
>>
>> [3, 5, 7]
>
> [ val for val, i in itertools.izip(a, itertools.count()) if not i % 2 ]
>>
>> [2, 4, 6]
>>
>> That could probably be written a bit nicer..
>>
>> On Thu, Dec 9, 2010 at 10:44 AM, Cal Leeming [Simplicity Media Ltd]
>>   wrote:
>>
>> mylist = [1,2,3,4,5,6,7,8]
>> filter(lambda x: x%2, mylist)
>>>
>>> [1, 3, 5, 7]
>>> 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 at 
http://groups.google.com/group/django-users?hl=en.



Re: Help for for loop

2010-12-09 Thread Cal Leeming [Simplicity Media Ltd]
Can't you use the % operator within if statements in templates? I was 
almost sure I'd done this before :|


On 09/12/2010 11:27, bruno desthuilliers wrote:

On 9 déc, 12:18, Phani Chand  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 at 
http://groups.google.com/group/django-users?hl=en.



Re: Help for for loop

2010-12-09 Thread Cal Leeming [Simplicity Media Ltd]

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=[2,3,4,5,6,7]
filter(lambda x: x%2, a)

[3, 5, 7]

[ val for val, i in itertools.izip(a, itertools.count()) if not i % 2 ]

[2, 4, 6]

That could probably be written a bit nicer..

On Thu, Dec 9, 2010 at 10:44 AM, Cal Leeming [Simplicity Media Ltd]
  wrote:

mylist = [1,2,3,4,5,6,7,8]
filter(lambda x: x%2, mylist)

[1, 3, 5, 7]
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 at 
http://groups.google.com/group/django-users?hl=en.



Re: Help for for loop

2010-12-09 Thread bruno desthuilliers
On 9 déc, 12:18, Phani Chand  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 at 
http://groups.google.com/group/django-users?hl=en.



Re: Help for for loop

2010-12-09 Thread Tom Evans
Hmm, those are the values that are odd, he wanted the values from odd
indices, eg:

>>> a=[2,3,4,5,6,7]
>>> filter(lambda x: x%2, a)
[3, 5, 7]
>>> [ val for val, i in itertools.izip(a, itertools.count()) if not i % 2 ]
[2, 4, 6]

That could probably be written a bit nicer..

On Thu, Dec 9, 2010 at 10:44 AM, Cal Leeming [Simplicity Media Ltd]
 wrote:
 mylist = [1,2,3,4,5,6,7,8]
 filter(lambda x: x%2, mylist)
> [1, 3, 5, 7]

>
> 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 at 
http://groups.google.com/group/django-users?hl=en.



Re: Help for for loop

2010-12-09 Thread Cal Leeming [Simplicity Media Ltd]

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 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.



Re: Help for for loop

2010-12-09 Thread Phani Chand
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 at 
http://groups.google.com/group/django-users?hl=en.



Re: Help for for loop

2010-12-09 Thread Cal Leeming [Simplicity Media Ltd]

>>> mylist = [1,2,3,4,5,6,7,8]
>>> filter(lambda x: x%2, mylist)
[1, 3, 5, 7]
>>>

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.