在python中将变量传递给dict

jhkqcmku  于 2023-06-25  发布在  Python
关注(0)|答案(1)|浏览(105)

我有以下简单的代码:

dataStore = {}

def addLocationToDataStore(location):
    dataStore = {location: {'teamChatMessageCount.staff': 0, 'privateChatMessageCount.staff': 0, 'callCount.staff': 0, 'meetingCount.staff': 0, 'meetingsOrganizedCount.staff': 0, 'adHocMeetingsOrganizedCount.staff': 0, 'adHocMeetingsAttendedCount.staff': 0, 'urgentMessages.staff': 0, 'postMessages.staff': 0, 'replyMessages.staff': 0, 'audioDuration.staff': 0, 'videoDuration.staff': 0, 'screenShareDuration.staff': 0,
                            'teamChatMessageCount.student': 0, 'privateChatMessageCount.student': 0, 'callCount.student': 0, 'meetingCount.student': 0, 'meetingsOrganizedCount.student': 0, 'adHocMeetingsOrganizedCount.student': 0, 'adHocMeetingsAttendedCount.student': 0, 'urgentMessages.student': 0, 'postMessages.student': 0, 'replyMessages.student': 0, 'audioDuration.student': 0, 'videoDuration.student': 0, 'screenShareDuration.student': 0}}
    

addLocationToDataStore("test") 

print(datastore)

目的是在调用addLocationToDataStore时创建嵌套字典。如果我对location变量进行硬编码,则该函数可以工作,但如果我使用is作为键,则无法工作。
我在看如果我需要使用'周围的位置(按照下面),但这是不工作(非法字符)

datastore = {\'location\': {

我该如何摆脱它或更动态地使用字典?

vc6uscn9

vc6uscn91#

我是有点傻与此,这是一个语法错误,应该是以下。

dataStore[location] = {'teamChatMessageCount.staff': 0

相关问题