我有一個函數可以接收兩個文件.txt,一個是任務,另一個是transaltors。 將已分配任務的列表返回給譯員。如何從字典中獲得特定值?
反正那不是重點,我試着去檢查,如果在翻譯字典中的給定值等於出現在任務文件,該文件是一個列表中的其他元素,這是者均基於功能:
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下如何寫的文件,這是用來讀取文件的功能IM:
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
@ Jean-FrançoisFabre我知道它仍然給我那個輸出,只是想要一部分的價值。 – Exzlanttt
嘗試'list(translators.values())[0]'或'next(translators.values().__ iter __())' –
@ Jean-FrançoisFabre第一個返回[in one line and] on next;第二個它只是空行 – Exzlanttt