我有一个文件我想在循环中运行。我的文件如下所示:
Hello Hello
A B C D
A B C D
B C D A
B C D A
C D A B
B C D A
Hello Bye
A C D B
C D A B
A C D B
D C A B
我只在“再见”和“空行”之间循环。你知道吗
它应该返回:
{(A, C, D, B): 2, (C, D, A, b):1, (D, C, A, B):1}
有没有一种方法我可以称之为“all”来添加所有的排列?你知道吗
我不想使用任何导入。你知道吗
到目前为止,我的函数如下所示:
# input_word = Hello or Bye
def file_to_dict (file, input_word):
new_dict = {}
new_list = []
flag = False
for line in f:
if 'Hello' in line:
continue
if not line.strip():
continue
lElts = tuple(line.split(' '))
if lElts in dRet:
dRet[lElts] += 1
else:
dRet[lElts] = 1
return dRet
有没有其他选择继续?你知道吗
这给了我一个错误:
ValueError: I/O operation on closed file
类似于这样,在
hello Bye
处拆分,然后使用dict.get()
向字典添加值。你知道吗有很多逻辑错误。。。我建议你查一下
elif
的工作原理相关问题 更多 >
编程相关推荐