Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

creating sets of tuples in python

Tags:

python

tuples

How do i create a set of tuples with each tuple containing two elements? Each tuple will have an x and y value: (x,y) I have the numbers 1 through 50, and want to assign x to all values 1 through 50 and y also 1 through 50.

S = {(1,1),(1,2),(1,3),(1,4)...(1,50),(2,1)......(50,50)}

I tried

positive = set(tuple(x,y) for x in range(1,51) for y in range(1,51))

but the error message says that a tuple only takes in one parameter. What do I need to do to set up a list of tuples?

like image 879
Chung Avatar asked Jan 25 '14 00:01

Chung


People also ask

Can I make a set of tuples?

To create a tuple in Python, use the parentheses ( ), separated by commas. The parentheses are optional. A tuple can have many elements with different data types like integer, float, list, string, etc. To create a set in Python, use the parentheses { }, separated by commas.

What are set of tuples in Python?

Tuple. Tuples are used to store multiple items in a single variable. Tuple is one of 4 built-in data types in Python used to store collections of data, the other 3 are List, Set, and Dictionary, all with different qualities and usage. A tuple is a collection which is ordered and unchangeable.

Can you create a list of tuples in Python?

We can create a list of tuples i.e. the elements of the tuple can be enclosed in a list and thus will follow the characteristics in a similar manner as of a Python list. Since, Python Tuples utilize less amount of space, creating a list of tuples would be more useful in every aspect.


Video Answer


1 Answers

mySet = set(itertools.product(range(1,51), repeat=2))

OR

mySet = set((x,y) for x in range(1,51) for y in range(1,51))
like image 124
inspectorG4dget Avatar answered Oct 16 '22 21:10

inspectorG4dget