Python 基于人员、项目和存储的分组
我担心的是我的代码没有在Python3.5中运行,它在Python2.7中运行得非常好 问题是我们需要根据以下内容对内容进行分组:Python 基于人员、项目和存储的分组,python,python-3.x,Python,Python 3.x,我担心的是我的代码没有在Python3.5中运行,它在Python2.7中运行得非常好 问题是我们需要根据以下内容对内容进行分组: 人 项目 贮藏 文件1511.txt中的内容包括: david book 8 walmart mike book 5 kroger david food 3 walmart mary food 11 target 输出: 'mike': 5, 'mary': 11, 'david': 11 'food': 14, 'book': 13 'kroger': 5,
1511.txt
中的内容包括:
david book 8 walmart
mike book 5 kroger
david food 3 walmart
mary food 11 target
输出:
'mike': 5, 'mary': 11, 'david': 11
'food': 14, 'book': 13
'kroger': 5, 'walmart': 11, 'target': 11
mike 5
mary 11
david 11
number of items sold by mike is 5
number of items sold by mary is 11
number of items sold by david is 11
total number of food sold 14
total number of book sold 13
total number of items sold by kroger is 5
total number of items sold by walmart is 11
total number of items sold by target is 11
当我尝试在python 3.5中运行相同的程序时,它给出了以下输出,并出现了错误:
'mike': 5, 'david': 11, 'mary': 11
'food': 14, 'book': 13
'target': 11, 'walmart': 11, 'kroger': 5
Traceback (most recent call last):
File "C:\Users\putch\AppData\Local\Programs\Python\Python35\1511prognew.py", line 20, in <module>
print(str(d.keys()[i]+" "+str(d.values()[i])))
TypeError: 'dict_keys' object does not support indexing
dict.keys()不再返回列表,编写打印语句的更简单方法是:
for k, v in d.items():
print("{} {}".format(k,v))
或者,您可以只使用list(dict.keys())
,但我展示的方法更清晰 dict.keys()不再返回列表,编写打印语句的更简单方法是:
for k, v in d.items():
print("{} {}".format(k,v))
或者,您可以只使用
list(dict.keys())
,但我展示的方法更清晰 当你说“Python3.7”时,我们是否假设你的意思是“Python3.5”?对不起,我理解Python3.5当你说“Python3.7”时,我们是否假设你的意思是“Python3.5”?对不起,我理解不了Python3.5。我无法理解它。