在python中使用string作为对象

时间:2013-08-19 11:05:41

标签: python set

被修改

好的,整个目标是找到给定物体中的最小纬度:

>>>g.nodes
set([v99, v192, v60, v107, v54, v75, v16, v93, v190, v125, v193, v218, v114, v1, v180, v68, v11, v86, v82, v58, v166, v230, v98, v18, v20, v72, v221, v177, v36, v19, v97, v27, v206, v153, v233, v169, v88, v53, v229, v77, v50, v34, v90, v162, v106, v89, v67, v173, v158, v123, v214, v165, v223, v199, v155, v149, v59, v138, v69, v204, v154, v91, v119, v70, v152, v61, v121, v195, v38, v17, v66, v23, v213, v45, v13, v62, v150, v137, v136, v76, v29, v64, v207, v234, v46, v113, v41, v122, v120, v83, v203, v148, v30, v33, v191, v32, v127, v131, v2, v57, None, v81, v157, v104, v226, v139, v79, v200, v100, v186, v170, v175, v109, v172, v132, v9, v160, v96, v10, v198, v52, v87, v14, v163, v51, v144, v228, v21, v215, v201, v212, v187, v135, v209, v232, v164, v126, v161, v22, v115, v6, v133, v85, v171, v12, v183, v182, v31, v42, v111, v55, v80, v35, v211, v143, v24, v95, v56, v112, v178, v231, v4, v194, v92, v124, v117, v151, v219, v146, v94, v168, v101, v220, v39, v8, v105, v44, v40, v216, v7, v208, v197, v145, v202, v185, v205, v116, v48, v222, v217, v188, v134, v47, v5, v73, v227, v108, v156, v28, v224, v210, v176, v3, v37, v74, v167, v181, v15, v159, v118, v63, v25, v102, v110, v196, v43, v174, v103, v225, v142, v179, v128, v84, v129, v147, v78, v184, v141, v140, v189, v71, v65, v130, v49, v26])
>>> v99
v99
>>> v99.latitude
46.933333
>>> type(g.nodes)
<type 'set'>
>>> type(v99)
<type 'org.gephi.scripting.wrappers.GyNode'>
>>> type(v99.latitude)
<type 'float'>

插件的文档:http://wiki.gephi.org/index.php/Scripting_Plugin

3 个答案:

答案 0 :(得分:2)

可以使用locals() function查找本地名称:

for i in data:
    print locals()[i]

但您确实想重新考虑您的数据结构,并且可能使用字典来存储xy

locals()将当前本地名称空间作为字典返回。您应该将此字典视为只读,尤其是在函数中。

答案 1 :(得分:2)

您可以使用locals()

In [1]: x = 'hello'

In [2]: y = 'worlds'

In [3]: data = set(['x', 'y'])

In [4]: for i in data:
   ...:     print locals()[i]
   ...:     
worlds
hello

但是,你最好构建一个字典,如:

data = {'x': 'hello', 'y': 'worlds'}

答案 2 :(得分:0)

  

我有一个对象列表

自称为列表的Python数据结构以来,文字list的对象如下所示:

objectlist = ['x', 'y']

您在代码中描述的内容不是对象列表,而是几个随机无关联的对象。

但是,很明显你不是在追踪对象列表之后。您希望能够查找“我标记为'x'的对象” - 正确的数据结构是Python调用dict - 其他语言称为映射,关联数组或散列表。然后,您可以使用data中的值来查找字典中的相应值。

>>> objectdict = {'x': 'hello', 'y': 'world'}
>>> data = set(['x', 'y'])
>>> for i in data:
...     print objectdict[i]
...
world
hello
>>>