如何获取字典的KEY值的引用? [英] How do I get a reference to a KEY value of a dictionary?

查看:142
本文介绍了如何获取字典的KEY值的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是python的新手,所以如果我做了一些

概念错误,请耐心等待。


基本上我想创建一个图表邻接列表

表示,但我不希望任何邻接列表在可避免的情况下具有重复字符串。我有一个函数createEdge

,它为图形添加了一个边缘。参数将是不同的,因为它们是从文本文件中读取的。但基本上我想使用

字典作为字符串池,如果参数字符串等于

池中的东西,不要使用参数字符串,只是一个

已经使用了对字符串池中某些内容的引用。


这是浪费时间吗?因为我知道在python中我无法确定

从文件中读取的参数字符串甚至是垃圾

无论如何都要收集。我当然可以用重复的

字符串完成这项工作,但这看起来很浪费。我是一名C程序员,旧的

习惯很难。


以下代码有效 - 我测试了它和邻接中的条目

等效的列表实际上是相同的。但似乎相当有一个字典{''alice'':''alice''''''bob'':

''bob ''}等,即键和值是相同的。如果我能在哈希查找的同时获得对密钥的引用,那将是很好的



我知道我可以做dictionary.keys()。index (''string'')或者其他东西但是

我认为效率非常低。


class GraphAccumulator:

def __init __(self,fileFunction):

self.fileFunction = fileFunction

self.adjList = {}#adjacency list

self.nodes = {} #b>节点列表

防止字符串欺骗

def createEdge(self,node1,node2):


#另一种使用字典的方法


nodes = self.nodes

if nodes.has_key(node2):

node2 = nodes [node2]

else:

nodes [node2] = node2


adjList = self.adjList

如果adjList.has_key(node1):

adjList [node1] .append(node2);

else:

adjList [node1 ] = [node2]; #singleton edge list

I am new to python, so please bear with me if I am making some
conceptual error.

Basically I want to create a graph with an adjacency list
representation, but I don''t want any of the adjacency lists to have
duplicate strings when it is avoidable. I have a function createEdge
that adds an edge to the graph. The arguments will be distinct since
they are read from text files. But basically I want to use the
dictionary as a string pool, and if the argument string equals
something in the pool already, don''t use the argument string, just a
use a reference to something in the string pool already.

Is this a waste of time? Because I know in python I cannot be certain
that the argument strings that are read from files are even garbage
collected anyway. I could certainly do the job with duplicate
strings, but it would seem wasteful. I am a C programmer, and old
habits die hard.

The following code works -- I tested it and entries in the adjacency
list that are equivalent are in fact identical. But it seems rather
stupid to have a dictionary of the form {''alice'': ''alice'', ''bob'':
''bob''}, etc. i.e. the keys and values are the same. It would be nice
if I can get a reference to the key in the same time as a hash lookup.
I know I can do dictionary.keys().index( ''string'' ) or something but
that would be pretty inefficient, I believe.

class GraphAccumulator:
def __init__( self, fileFunction ):
self.fileFunction = fileFunction
self.adjList = {} # adjacency list
self.nodes = {} # list of nodes for
preventing string dupes

def createEdge( self, node1, node2 ):

# another way of doing by using a dictionary

nodes = self.nodes
if nodes.has_key( node2 ):
node2 = nodes[ node2 ]
else:
nodes[ node2 ] = node2

adjList = self.adjList
if adjList.has_key( node1 ):
adjList[ node1 ].append( node2 );
else:
adjList[ node1 ] = [ node2 ]; # singleton edge list

推荐答案

2003年7月31日17:36:41 -0700,Andy C写道:
On 31 Jul 2003 17:36:41 -0700, Andy C wrote:
是这浪费时间?因为我知道在python中我不能确定从文件中读取的参数字符串甚至是垃圾收集。我当然可以使用重复的
字符串来完成这项工作,但这看起来很浪费。我是一名C程序员,旧的习惯很难过。
Is this a waste of time? Because I know in python I cannot be certain
that the argument strings that are read from files are even garbage
collected anyway. I could certainly do the job with duplicate
strings, but it would seem wasteful. I am a C programmer, and old
habits die hard.




Python动态地将名称(变量)绑定到对象;许多类型,包括不可变字符串的
,如果已存储相同的

,则不会重复。


结果是:根据需要多次存储相同的字符串,

因为PYthon只会保留对每个重复的

的一个字符串对象的引用。

-

\友谊诞生于那个时候,一个人说|

` \ another,''什么!你也是?我以为我是唯一的一个!''' - |

_o__)CS Lewis |

Ben Finney< http://bignose.squidly.org/>



Python dynamically binds names ("variables") to objects; many types,
including immutable strings, will not be duplicated if an identical one
is already stored.

The upshot is: store an identical string as many times as you like,
because PYthon will simply keep references to the one string object for
each duplicate.

--
\ "Friendship is born at that moment when one person says to |
`\ another, ''What! You too? I thought I was the only one!''" -- |
_o__) C.S. Lewis |
Ben Finney <http://bignose.squidly.org/>




" Andy C" <一个****** @ yahoo.com>在消息中写道

news:64 ************************** @ posting.google.c om ...

"Andy C" <an******@yahoo.com> wrote in message
news:64**************************@posting.google.c om...
我是python的新手,所以如果我做了一些
概念错误,请耐心等待。

基本上我想创建一个带有邻接列表的图形
表示,但我不希望任何邻接列表在可避免的情况下具有重复的字符串。我有一个函数
createEdge,它为图形添加了一个边缘。因为它们是从文本文件中读取的,所以参数将是不同的
。但基本上我想用
字典作为字符串池,如果参数字符串等于池中的东西,不要使用参数字符串,只需要使用一个
已经在字符串池中引用某些内容。
I am new to python, so please bear with me if I am making some
conceptual error.

Basically I want to create a graph with an adjacency list
representation, but I don''t want any of the adjacency lists to have
duplicate strings when it is avoidable. I have a function createEdge that adds an edge to the graph. The arguments will be distinct since they are read from text files. But basically I want to use the
dictionary as a string pool, and if the argument string equals
something in the pool already, don''t use the argument string, just a
use a reference to something in the string pool already.




根据引用进行思考可以帮助和阻碍。 (最近有一段时间讨论过
。)


你熟悉这个吗?



Thinking in terms of ''references'' can both help and hinder. (There
have been some long recent discussion.)

Are you familiar with this?

help(''intern'')
help(''intern'')




在职能实习生:


实习生(...)

实习生(字符串) - > string


``Intern''''给定的字符串。这将输入

(全局)

实习字符串表中的字符串,其目的是加速字典

查找。

返回字符串本身或以前的实习字符串对象



相同的值。


TJR



Help on built-in function intern:

intern(...)
intern(string) -> string

``Intern'''' the given string. This enters the string in the
(global)
table of interned strings whose purpose is to speed up dictionary
lookups.
Return the string itself or the previously interned string object
with the
same value.

TJR


在很多情况下我不认为这是真的。例如:
I don''t think this is true in many cases. For example:
x =''hi''
y =''hi''
x是y
真a =''hi''
b =''''
b + =''我'
b
''hi''a是b
x = ''hi''
y = ''hi''
x is y True a = ''hi''
b = ''h''
b += ''i''
b ''hi'' a is b


False


此外,当字符串来自使用readline()读入的不同文件时,

似乎重复没有消除。我尝试了没有

哈希表重复消除的代码,并且邻接列表具有不相同的

字符串。在我构建了邻接列表之后,我将它们与''是''

进行了比较,它们并不相同。在我使用哈希表黑客解决方案后,所有

等效字符串都是相同的。


Ben Finney <双**************** @ and-benfinney-does-too.id.au>写在

消息新闻:sl ******************************* @ iris.polar。本地... 2003年7月31日17:36:41 -0700,Andy C写道:

False

Also, when the strings come from different files read in using readline(),
it appears that duplicates aren''t eliminated. I tried my code without the
hash table duplicate elimination, and the adjacency lists has non-identical
strings. After I constructed the adjacency lists, I compared them with ''is''
and they were not identical. After I used my hash table hack solution, all
equivalent strings were identical.

"Ben Finney" <bi****************@and-benfinney-does-too.id.au> wrote in
message news:sl*******************************@iris.polar. local... On 31 Jul 2003 17:36:41 -0700, Andy C wrote:

这是浪费时间吗?因为我知道在python中我不能确定从文件中读取的参数字符串甚至是垃圾收集。我当然可以使用重复的
字符串来完成这项工作,但这看起来很浪费。我是一名C程序员,旧的习惯很难过。
Is this a waste of time? Because I know in python I cannot be certain
that the argument strings that are read from files are even garbage
collected anyway. I could certainly do the job with duplicate
strings, but it would seem wasteful. I am a C programmer, and old
habits die hard.



Python动态地将名称(变量)绑定到对象;许多类型,包括不可变字符串,如果已经存储了相同的字符串,则不会重复。

结果是:根据需要多次存储相同的字符串,
因为PYthon会简单地保留对每个副本的一个字符串对象的引用。

-
\友谊诞生于那个时刻,一个人对另一个人说,什么!你也是?我以为我是唯一的一个!''' - |
_o__)CS Lewis |
Ben Finney< http://bignose.squidly.org/>



Python dynamically binds names ("variables") to objects; many types,
including immutable strings, will not be duplicated if an identical one
is already stored.

The upshot is: store an identical string as many times as you like,
because PYthon will simply keep references to the one string object for
each duplicate.

--
\ "Friendship is born at that moment when one person says to |
`\ another, ''What! You too? I thought I was the only one!''" -- |
_o__) C.S. Lewis |
Ben Finney <http://bignose.squidly.org/>



这篇关于如何获取字典的KEY值的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆