gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:51:45 25 4
gpt4 key购买 nike

我有一个函数可以接收两个文件.txt,一个包含任务,另一个包含转换器。返回分配给翻译人员的任务列表。

无论如何,这不是重点,我试图检查翻译字典上的给定值是否等于任务文件中存在的另一个元素(列表),这是函数顺便说一句:

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() 但这会返回所有值,例如(这是一个键的值,仅作为示例):

[' (portuguese; french)', ' (english)', ' 3*', ' 0.803', ' 3000', ' 25000', ' 3084', ' 08:11:2016\\n']

,但我只想要(葡萄牙语;法语)位,如果尝试运行类似的东西

for translator in translators.values():
print translator[0]

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

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

这是输入文件:

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

最佳答案

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

translatorDict[key] = str(value)

我会这样做:

  • 分割线
  • 丢弃 header (字段不足:列表索引超出范围)
  • 将字典值存储为 token 列表,更加灵活

代码:

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)

然后:

for translator in translators.values():
print(translator[0].strip().replace(")","").replace("(",""))

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

关于python - 如何从字典中获取特定值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41223342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com