关于Test Dome的问题。你知道吗
实施“按所有者分组”功能:
接受包含每个文件名的文件所有者名称的字典。你知道吗
返回一个字典,其中包含每个所有者名称的文件名列表,按任意顺序排列。你知道吗
例如,对于dictionary {'Input.txt': 'Randy', 'Code.py': 'Stan', 'Output.txt': 'Randy'}
,group_by_owners
函数应该返回{'Randy': ['Input.txt', 'Output.txt'], 'Stan': ['Code.py']}
。你知道吗
当我运行我在做示例测试时开发的下面的代码时,他们的在线应用程序给了我0%的分数,如果你在IDE中运行下面的代码,它会输出正确的转换字典。你知道吗
为什么他们的网站没有为开发的测试打分呢?这是他们的算法的一个缺陷,可以检测他们网站上的代码是否正确?还是我做的不对?你知道吗
我对Python的理解大约有2年的经验。你知道吗
'''Find all unique names given a dict'''
def getUniqueNames(file):
file_owner_names = []
for file_type, file_o in file.items():
if file_o not in file_owner_names:
file_owner_names.append(file_o)
return file_owner_names
'''Get a list of files for an owner given a dict and owner'''
def getFileArray(file, file_owner):
file_type_names = []
for file_t, file_o in file.items():
if file_o == file_owner:
if file_t not in file_type_names:
file_type_names.append(file_t)
return file_type_names
'''Learned to used the dict'''
def group_by_owners(files_dict):
new_file = {}
i = 0
file_owner_names = getUniqueNames(files_dict)
for file_owner_name in file_owner_names:
if i != len(file_owner_names):
if (i < len(file_owner_names)):
new_file[file_owner_name] = str(getFileArray(files_dict,
file_owner_name))
i = i + 1
return new_file
files = {
'Input.txt': 'Randy',
'Code.py': 'Stan',
'HomeController.py': 'Randy',
'Output.txt': 'Jeff',
'SearchController.py': 'Rafeena',
'ABTest.py': 'Nicholas',
'SQL.py':'Nicholas'
}
print(group_by_owners(files))
上述python脚本的输出:
{'Randy': "['Input.txt', 'HomeController.py']", 'Stan': "['Code.py']", 'Jeff': "['Output.txt']", 'Rafeena': "['SearchController.py']", 'Nicholas': "['ABTest.py', 'SQL.py']"}
我还没测试过,但我认为你的问题是你应该改变
至
通过删除对
str
的调用。你知道吗输出应为:
注意列表周围缺少引号。你知道吗
您可以从集合中使用defaultdict。Defaultdict负责添加新密钥。你知道吗
不完全回答你的问题,但是这个解决方案使用标准字典。你知道吗
我的答案基于一个例子:StackOverflow
相关问题 更多 >
编程相关推荐