Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/349.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.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_Function_Set - Fatal编程技术网

Python 如何在集合中追加函数返回的值?

Python 如何在集合中追加函数返回的值?,python,function,set,Python,Function,Set,我编写了一个代码,它将两个集合(tag_坐标,tag_id)返回的值从中的函数更新为单个集合(必需的信息)。当我试图打印required_info中的值时,该值会打印两次,并且集合中的元素没有顺序 def GetLabels(taginfo): #this function gets the value taginfo, from an another code tag_coordinates = set() #set to update tag_coordinates

我编写了一个代码,它将两个集合(tag_坐标,tag_id)返回的值从中的函数更新为单个集合(必需的信息)。当我试图打印required_info中的值时,该值会打印两次,并且集合中的元素没有顺序

def GetLabels(taginfo):      #this function gets the value taginfo, from an another code
    tag_coordinates = set()  #set to update tag_coordinates
    tag_id = set()           #set to update tag_id
    required_info = set()    #set to update required_info
    r=60
    for (tag,xy,orient,err,wl,sq) in taginfo:
        xy2= (int(xy[0]+r*math.sin(math.radians(orient))),int(xy[1]-r*math.cos(math.radians(orient))))
        tag_coordinates.add(xy) #corresponding tag_coordinates
        tag_id.add(tag)         #corresponding tag_id
        required_info.update(tag_coordinates,tag_id)
        print required_info  

现在,一旦我打印了所需的标签信息,我将得到一个打印两次的集合,对应的值来自集合标签坐标,标签id没有排列在一起。有什么建议吗?

我不确定你想达到什么目的,但如果你想要一组配对(标签、坐标)

如果一个tagid可以有多个Cooridante,那么可以使用groupby

groupby(operator.itemgetter(0),get_labels(taginfo))
将按标记对坐标进行分组

get_labels
只是
GetLabels
,但采用符合PEP8的表示法。函数应使用小写字母,并且应使用snake_而不是CamelCase

calc_坐标
只是从此行提取的函数

xy2= (int(xy[0]+r*math.sin(math.radians(orient))),int(xy[1]-r*math.cos(math.radians(orient))))
同样,它不符合PEP8,可读性也不太好。
orient\u sin
只是一个临时变量,用于提高性能和可读性。因为我不知道这个域名,我可能给它起了一个丑陋的名字,但是你可以改进它


get_labels
返回一组对(标记id、坐标)。它是通过python的集合理解功能定义的。

set不必在数学中排序,所以在python中是这样。我无法理解您提供的内容。你能提出一些意见吗?我试图在代码中实现的是将(tag_坐标,tag_id)对集合在一起。我能用上面提到的代码实现它。但同样的值被打印了两次。@George luke我得到了一组对(标记id,标记坐标)的意思。我不明白的是你所说的一起
是什么意思。我的意思是与tag\u id相对应的tag\u坐标。那么,我想我答案中的代码就是你需要的。我在
标记(x,y),方向,*.\uu
中使用集合理解和iterable解包<代码>*.
将是包含taginfo中其余元素的元组。这只是一种表示您不关心该部分的方式。如果一个
tag\u id
可以有多个
tag\u坐标
,那么您可以使用groupby。
xy2= (int(xy[0]+r*math.sin(math.radians(orient))),int(xy[1]-r*math.cos(math.radians(orient))))