在python中创建元组集 [英] creating sets of tuples in python
本文介绍了在python中创建元组集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何创建一组元组,每个元组包含两个元素?每个元组都有一个 x
和 y
值:(x,y)
我有数字 1 到 50,并且想将 x
分配给 1 到 50 的所有值,y
也分配 1 到 50.
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)}
我试过了
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?
推荐答案
mySet = set(itertools.product(range(1,51), repeat=2))
或
mySet = set((x,y) for x in range(1,51) for y in range(1,51))
这篇关于在python中创建元组集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文