首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从字典中获取特定值?

如何从字典中获取特定值?
EN

Stack Overflow用户
提问于 2016-12-19 21:10:21
回答 1查看 68关注 0票数 1

我有一个接收两个文件.txt的函数,一个包含任务,另一个包含transaltors。向翻译人员返回已分配任务的列表。

无论如何,这不是重点,我正在尝试检查翻译器字典上给定的值是否等于任务文件中存在的另一个元素,即列表,这是函数btw:

代码语言:javascript
复制
def scheduleTasks(translators, tasks, date, hour, periodAfter):

    """Assigns translation tasks to translators.

    Requires:
    translators is a dict with a structure as in the output of
    readingFromFiles.readTranslatorsFile concerning the update time; 
    tasks is a list with the structure as in the output of 
    readingFromFiles.readTasksFile concerning the period of
    periodAfter minutes immediately after the update time of translators;
    date is string in format DD:MM:YYYY with the update time;
    hour is string in format HH:MN: with the update hour;
    periodAfter is a int with the number of minutes of a period 
    of time elapsed.
    Ensures:
    a list of translation tasks assigned according to the conditions
    indicated in the general specification (omitted here for 
    the sake of readability).
    """

我的问题是如何从字典中获取值,我知道d.values(),但它返回的所有值如下(这是一个键的值,仅作为示例):

代码语言:javascript
复制
[' (portuguese; french)', ' (english)', ' 3*', ' 0.803', ' 3000', ' 25000', ' 3084', ' 08:11:2016\\n']

,但我只想要一点(葡萄牙语;法语),如果尝试运行像这样的东西

代码语言:javascript
复制
for translator in translators.values():
      print translator[0]

它返回'[‘,我不确定,但我认为这是由于linux写入文件的方式,这是Im用来读取文件的函数:

代码语言:javascript
复制
def readTranslatorsFile(file_name):
    """Reads a file with a list of translators into a collection.

    Requires:
    file_name is str with the name of a .txt file containing
    a list of translators organized as in the examples provided in
    the general specification (omitted here for the sake of readability).
    Ensures:
    dict where each item corresponds to a translator listed in
    file with name file_name, a key is the string with the name of a translator,
    and a value is the list with the other elements belonging to that
    translator, in the order provided in the lines of the file.
    """
    inFile = removeHeader(file_name)       

    translatorDict = {}
    for line in inFile:

        key = line.split(",")[INDEXTranslatorName]
        value = line.split(",")[1::]
        translatorDict[key] = str(value)
    return translatorDict

这是输入文件:

代码语言:javascript
复制
Company:
ReBaBel
Day:
07:11:2016
Time:
23:55
Translators:
Ana Tavares, (english), (portuguese), 1*, 0.501, 2000, 20000, 2304, 08:11:2016
Mikolás Janota, (czech), (english; portuguese), 3*, 1.780, 2000, 200000, 4235, 08:11:2016
Paula Guerreiro, (french), (portuguese), 2*, 0.900, 3500, 45000, 21689, 11:11:2016
Peter Wittenburg, (dutch; english), (dutch; english), 2*, 1.023, 2500, 20000, 7544, 08:11:2016
Rita Carvalho, (english), (portuguese), 1*, 0.633, 5000, 400000, 18023, 09:11:2016
Steven Neale, (portuguese; french), (english), 3*, 0.803, 3000, 25000, 3084, 08:11:2016
EN

回答 1

Stack Overflow用户

发布于 2016-12-19 21:20:08

问题是,当您解析文件时,您使用str.split()创建了一个列表,这很好,但随后您将该列表转换回字符串,而不是将list保留为您的值。那是很糟糕的。

代码语言:javascript
复制
translatorDict[key] = str(value)

我会这么做的:

  • 拆分行
  • 丢弃标题(字段不足:列表索引超出范围)
  • 将字典值存储为标记列表,更灵活的

代码:

代码语言:javascript
复制
def readTranslatorsFile(file_name):

    inFile = removeHeader(file_name)       

    translatorDict = {}
    for line in inFile:
        tokens = line.split(",")  # split once and for good
        if len(tokens)>1:
          key = tokens[INDEXTranslatorName]
          value = tokens[1:]  # all items but first
          translatorDict[key] = value
    return translatorDict

(或者使用csv模块更好地处理逗号分隔的文件(引号等))

请注意,如果INDEXTranslatorName不为零,那么您的方法将失败,那么您可以编写value = tokens[:INDEXTranslatorName]+tokens[INDEXTranslatorName+1:]tokens.pop(INDEXTranslatorName)

然后:

代码语言:javascript
复制
for translator in translators.values():
      print(translator[0].strip().replace(")","").replace("(",""))

高效地打印您的语言元组。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41223342

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档