You are not logged in.

Change Language:

Freelancer? Consultant? Check out: WorkTrail - Time Tracking made easy
Board » Django » Python Programming » How to filter List Elements

Hi,

I am wrking on small project using Python2.6.Finally my list is like this.

['1:Rammmm,mmmm', '2:surr.rrrrr', '3:Sam ttttt',
'1:qqqq qqqqqqq', '2:wwwwwwwwwww', '3:eeeeeeeee','4:tttttttt',
'1:zzzzzzzzzzzzz', '2:xxxxxxxxx']

Here, i want to display all 1's in one line,all 2's in one line...etc, with pipe"|" as separator.It should be like this.

{Rammmm,mmmm|qqqq qqqqqqq|zzzzzzzzzzzzz}
{surr.rrrrr|wwwwwwwwwww|xxxxxxxxx}
{Sam ttttt|eeeeeeeee}
{tttttttt}

How to achieve this in Python.

Thanks.
Something like this.

def filter_list(query, data)
    temp = []
    for item in data:
        if item.split(':')\[0\] == query:
            temp.add(item)
    return temp

mylist = ['1:Rammmm,mmmm', '2:surr.rrrrr', '3:Sam ttttt', '1:qqqq qqqqqqq', '2:wwwwwwwwwww', '3:eeeeeeeee','4:tttttttt', '1:zzzzzzzzzzzzz', '2:xxxxxxxxx'] 
myfilteredlist = filter_list('2', mylist)
 
Sorry - use .append() instead of .add() and remove slashes before square brackets. I wrote this script ny hand - without python.
Thanks for your reply. aabele .

But my resulting stuff should be something like this.

{Rammmm,mmmm|qqqq qqqqqqq|zzzzzzzzzzzzz}
{surr.rrrrr|wwwwwwwwwww|xxxxxxxxx}
{Sam ttttt|eeeeeeeee}
{tttttttt}

ThanQ.
hgi thanks for your information





Powered by Sphene Community Tools