Needed help for Converting bag into tuple

This is the place for queries that don't fit in any of the other categories.

Needed help for Converting bag into tuple

Postby bashaone » Fri Nov 15, 2013 6:24 am

I have a below scenario

My input is as below
Code: Select all
{(123,345),(123,678),(123,900)}
{(213,567),(213,456)}

I need below output
Code: Select all
345,678,900
567,456
Last edited by Mekire on Fri Nov 15, 2013 7:55 am, edited 2 times in total.
Reason: Locked OP.
bashaone
 
Posts: 1
Joined: Fri Nov 15, 2013 6:18 am

Re: Needed help for Converting bag into tuple

Postby micseydel » Fri Nov 15, 2013 7:03 am

What have you tried? Have you read this?
Join the #python-forum IRC channel on irc.freenode.net!
User avatar
micseydel
 
Posts: 1128
Joined: Tue Feb 12, 2013 2:18 am
Location: Mountain View, CA

Re: Needed help for Converting bag into tuple

Postby Somelauw » Fri Nov 15, 2013 5:38 pm

bashaone wrote:I have a below scenario

My input is as below
Code: Select all
{(123,345),(123,678),(123,900)}
{(213,567),(213,456)}

I need below output
Code: Select all
345,678,900
567,456


Things between "{" and "}" are not bags but sets in python. You can check wikipedia to learn the difference.
It seems to me that you have a set of tuples and want to create a list containing the second element of each tuple.
This can very easily be done by simple using a for loop (possibly even a list comprehension), and then appending the second element of the tuple each time to an output list.
Please give it a try and it if doesn't work out, then post your code and your thoughts.
Join the #python-forum IRC channel on irc.freenode.net!
Somelauw
 
Posts: 68
Joined: Tue Feb 12, 2013 8:30 pm

Re: Needed help for Converting bag into tuple

Postby rrashkin » Sat Nov 16, 2013 3:22 pm

Just a small point and not germane to the original question: I don't think "Things between "{" and "}" are not bags but sets in python" is strictly true. I believe sets can only made by explicit conversion from lists or tuples (at least, that's how I've always made them) and cannot be defined declaritively. Things between curly braces in Python are dictionaries and have an entirely different structure.
___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Re: Needed help for Converting bag into tuple

Postby Mekire » Sat Nov 16, 2013 3:29 pm

rrashkin wrote: I believe sets can only made by explicit conversion from lists or tuples (at least, that's how I've always made them) and cannot be defined declaritively.

This is not true.
Code: Select all
>>> a = {1,2,3}
>>> type(a)
<type 'set'>

You can even use comprehension syntax:
Code: Select all
>>> a = {i**2 for i in range(5)}
>>> a
set([0, 1, 4, 9, 16])
>>> type(a)
<type 'set'>

If you want to declare an empty set on the other hand you must write:
Code: Select all
a = set()
as a pair of empty braces will, of course, make an empty dictionary.

-Mek
User avatar
Mekire
 
Posts: 983
Joined: Thu Feb 07, 2013 11:33 pm
Location: Amakusa, Japan

Re: Needed help for Converting bag into tuple

Postby rrashkin » Sun Nov 17, 2013 1:31 am

Sorry. I'm using a 2.5 shell on my Chromebook. It must be a newer feature than that.
___________________
Bob Rashkin
User avatar
rrashkin
 
Posts: 35
Joined: Tue Feb 12, 2013 4:47 pm

Re: Needed help for Converting bag into tuple

Postby stranac » Sun Nov 17, 2013 1:58 am

Yeah, I believe set literals were introduced in 2.7
Friendship is magic!

R.I.P. Tracy M. You will be missed.
User avatar
stranac
 
Posts: 1093
Joined: Thu Feb 07, 2013 3:42 pm


Return to General Coding Help

Who is online

Users browsing this forum: Bing [Bot] and 2 guests