Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中更新集合中元组的值_Python_Python 3.x_Set - Fatal编程技术网

在python中更新集合中元组的值

在python中更新集合中元组的值,python,python-3.x,set,Python,Python 3.x,Set,目前我有一个代码,它接受一组元组,每个元组的格式是(a,B)。在集合的整个过程中,每个A都有一个与其链接的多个B。我想创建另一个集合,其中包含格式为(A,B1,B2,B3)的元组,其中B1,B2,B3等是与第一个集合中的A相关联的B的值。目前我有以下代码: data_set = set(tuple(x) for x in data) #converts given list of lists to set of tuples association = set() #empty set to a

目前我有一个代码,它接受一组元组,每个元组的格式是(a,B)。在集合的整个过程中,每个A都有一个与其链接的多个B。我想创建另一个集合,其中包含格式为(A,B1,B2,B3)的元组,其中B1,B2,B3等是与第一个集合中的A相关联的B的值。目前我有以下代码:

data_set = set(tuple(x) for x in data) #converts given list of lists to set of tuples
association = set() #empty set to add (A, B1, B2, etc.) tuples
    for j in data_set: #loop through data set
        if (j[0]) not in association:
            associaton.add((j[0])) #This makes the first value of of my new tuple the value I want to find
        else:
           #I want to replace the current tuple with the current tuple plus the value of j[1]

如何在关联中找到要更新的正确元组。我计划使用a=a+(j[1])来更新元组a

您不能更改元组内的值,但您可以做的是将其设置为列表,然后将其设置为元组。大概是这样的:

tupl = ("a", "b", "c")
tupl = list(tupl)   # will change it to a list: ["a", "b", "c"]
tupl.append("d")    # appends "d" to the list
tupl = tuple(tupl)  # changes it back to a tuple ("a", "b", "c", "d")
association = {}  # empty dictionary
for j in data_set:
    if j[0] not in association:
        association[j[0]] = []  # initialize empty list
    association[j[0]].append(j[1])

您不能更改元组内的值,但您可以做的是将其设置为列表并在之后设置为元组。大概是这样的:

tupl = ("a", "b", "c")
tupl = list(tupl)   # will change it to a list: ["a", "b", "c"]
tupl.append("d")    # appends "d" to the list
tupl = tuple(tupl)  # changes it back to a tuple ("a", "b", "c", "d")
association = {}  # empty dictionary
for j in data_set:
    if j[0] not in association:
        association[j[0]] = []  # initialize empty list
    association[j[0]].append(j[1])

我会探出窗外,声称你在描述一场灾难

看起来
tuple
不是您要查找的数据结构。如果您有一个
(a,B)
格式的元组列表,并希望按它们的第一项对它们进行分组,则可以使用如下方法:

tupl = ("a", "b", "c")
tupl = list(tupl)   # will change it to a list: ["a", "b", "c"]
tupl.append("d")    # appends "d" to the list
tupl = tuple(tupl)  # changes it back to a tuple ("a", "b", "c", "d")
association = {}  # empty dictionary
for j in data_set:
    if j[0] not in association:
        association[j[0]] = []  # initialize empty list
    association[j[0]].append(j[1])
如果您的输入数据是
(A,B1)、(A,B2)、(A,B3)
,则结果将是
{A:[B1,B2,B3]}

使用元组解包可以稍微清除上面的代码:

association = {}  # empty dictionary
for key, value in data_set:
    if key not in association:
        association[key] = []  # initialize empty list
    association[key].append(value)

我会探出窗外,声称你在描述一场灾难

看起来
tuple
不是您要查找的数据结构。如果您有一个
(a,B)
格式的元组列表,并希望按它们的第一项对它们进行分组,则可以使用如下方法:

tupl = ("a", "b", "c")
tupl = list(tupl)   # will change it to a list: ["a", "b", "c"]
tupl.append("d")    # appends "d" to the list
tupl = tuple(tupl)  # changes it back to a tuple ("a", "b", "c", "d")
association = {}  # empty dictionary
for j in data_set:
    if j[0] not in association:
        association[j[0]] = []  # initialize empty list
    association[j[0]].append(j[1])
如果您的输入数据是
(A,B1)、(A,B2)、(A,B3)
,则结果将是
{A:[B1,B2,B3]}

使用元组解包可以稍微清除上面的代码:

association = {}  # empty dictionary
for key, value in data_set:
    if key not in association:
        association[key] = []  # initialize empty list
    association[key].append(value)

元组
是不可变的。。。i、 你不能改变它。您可以使用
列表(x)
(j[0])
不是元组;它只是在分组括号中的
j[0]
(j[0],)
将是一个元组。
元组
是不可变的。。。i、 你不能改变它。您可以使用
列表(x)
(j[0])
不是元组;它只是在分组括号中的
j[0]
(j[0],)
将是一个元组。您可以使用
集合。defaultdict
列表一起使用:
关联=defaultdict(列表)
,它将删除
if
。您可以使用
集合。defaultdict
列表一起使用:
关联=defaultdict(列表)
如果
,将删除