Python数组语法

2024-10-03 11:12:08 发布

您现在位置:Python中文网/ 问答频道 /正文

也许这是一个愚蠢/简单的问题,但是我应该如何在python中解释这种类型的数组表示法呢。你知道吗

self.sessions[(recepientId, deviceId)]

我想把一些Python翻译成PHP。你知道吗

正如有人说的不清楚,对我来说更不清楚,所以我会把全部功能。但我不能做更多,我不太了解Python。你知道吗

   def loadSession(self, recepientId, deviceId):
    if self.containsSession(recepientId, deviceId):
        return SessionRecord(serialized=self.sessions[(recepientId, deviceId)])
    else:
        return SessionRecord()

sessions变量的定义如下。你知道吗

self.sessions = {}

Tags: self功能类型returnifdef数组php
2条回答

此代码只是试图在中的其他代码中查找SessionRecord自我会话(它是键值映射)by key,其中key是recipientId和deviceId的一对。你知道吗

<>在C++、爪哇、C等中,人们宁愿写一些类似的东西:

class Key {
//constructor
Id recipientId;
Id deviceId;
//getters|setters and stuff
}
Map<Key, SessionRecord> sessions = Store.getSessions();
SessionRecord session = sessions.get(new Key(recipientId, deviceId));

(recepientId, deviceId)是由两个对象组成的元组。 self.sessions是一个字典(即键值哈希映射)。你知道吗

元组是,通过执行self.sessions[(recepientId, deviceId)]从字典中检索相关值。 因此,它只是在哈希映射(在Python中称为字典)中查找特定值。你知道吗

相关问题 更多 >