1>创建一个嵌套词典,其中包含您在秋季和春季学期中今年正在做的学科的学科编号。换句话说,您应该有一个带有2个键的字典“秋天”和“春天”,并且与这些键关联的值本身应该是字典,其中键是主题编号,值是主题名称。>
2>编写一个for循环,打印出您在秋天参加的科目编号。
这就是我所拥有的
my_subjects = {"Autumn": {37315:"Data", 34567:"Sci"}, "Spring": {23456:"Eng", 45879:"Math"}}
for season, season.subjects in my_subjects.items():
print("\n Autumn Subject Numbers", season)
for key in season.subjects:
print(key)
AttributeError Traceback (most recent call last)
<ipython-input-208-b1fceae351e6> in <module>()
5
6
----> 7 for season, season.subjects in my_subjects.items():
8 print("\n Autumn Subject Numbers", season)
9
AttributeError: 'str' object has no attribute 'subjects'
答案 0 :(得分:2)
使用public class ParentController {
private DataManager dataManager = new DataManager();
private ChildController childController1;
private ChildController childController2;
public void initializeChild() {
childController1.setDataManager(dataManager);
childController2.setDataManager(dataManager);
// Controllers for different FXML Files
//
}
}
public class ChildController {
/* Child controller has elements which need data
from the DataManager
*/
private DataManager dataManager;
public void setDataManager(DataManager dataManager) {
this.dataManager = dataManager;
}
}
中的.
运算符,您正在尝试访问season.subjects
对象的subjects
属性,该属性没有这样的属性。您应该将season
返回的元组中第二个项目的值分配给一个单独的变量:
my_subjects.items()
这将输出:
for season, subjects in my_subjects.items():
if season == 'Autumn':
print("Autumn Subject Numbers:", ', '.join(map(str, subjects)))
答案 1 :(得分:0)
尝试一下
my_subjects = {"Autumn": {37315:"Data", 34567:"Sci"}, "Spring": {23456:"Eng", 45879:"Math"}}
for season, data in my_subjects.items():
print("\n Autumn Subject Numbers", season)
for key in data:
print(key)
答案 2 :(得分:0)
如何?
new_dict = {}
for k, v in my_subjects.items():
for x, z in v.items():
if k not in new_dict:
new_dict[k] = [x]
else:
new_dict[k].append(x)
print(new_dict)
{'Autumn': [37315, 34567], 'Spring': [23456, 45879]}