Python 词典输出问题
Python Dictionary output issues
我是 python 和这个论坛的新手。在线学习对我不起作用,所以我不能只去找家教。这可能是我忘记的小事。我欢迎你能给我的任何帮助。
我试图让输出看起来像这样:她的名字是 Emmylou;她有望在 2021 年秋季毕业;她的账单已经付清;她的专业是考古学;她属于这些学校俱乐部——摄影、表演和欢乐合唱团
Emm = {'name' : 'Emmylou', 'graduate' : 'Fall 2021', 'bill' : 'paid', 'major' : 'Archeology', 'clubs-' : 'Photography, Acting and Glee'}
for Key, Value in Emm.items():
print(f"Her {Key} is {Value} and she is on track to {Key} in {Value}; Her {Key} is {Value}; Her {Key} is {Value}; She belongs to these school {Key} {Value}")
输出一团糟,当我 运行 时看起来像这样:
Her name is Emmylou and she is on track to name in Emmylou; Her name is Emmylou; Her name is Emmylou; She belongs to these school name Emmylou
Her graduate is Fall 2021 and she is on track to graduate in Fall 2021; Her graduate is Fall 2021; Her graduate is Fall 2021; She belongs to these school graduate Fall 2021
Her bill is paid and she is on track to bill in paid; Her bill is paid; Her bill is paid; She belongs to these school bill paid
Her major is Archeology and she is on track to major in Archeology; Her major is Archeology; Her major is Archeology; She belongs to these school major Archeology
Her clubs- is Photography, Acting and Glee and she is on track to clubs- in Photography, Acting and Glee; Her clubs- is Photography, Acting and Glee; Her clubs- is Photography, Acting and Glee; She belongs to these school clubs- Photography, Acting and Glee
在您的代码中,您将迭代数据中的每个 key-value 对;所以你最终打印了 5 次,每次都是 key-values 对,而不是打印 1 次,所有 key-value 对。
试试这个。
Emm = [
('name', 'Emmylou'),
('graduate', 'Fall 2021'),
('bill', 'paid'),
('major', 'Archeology'),
('clubs-', 'Photography, Acting and Glee'),
]
flat_items = [item for pair in Emm for item in pair]
print("Her {} is {} and she is on track to {} in {}; Her {} is {}; Her {} is {}; She belongs to these school {} {}".format(*flat_items))
首先,我假设您实际上已经在代码中缩进了 print 语句,否则它根本无法工作。
问题是对于每个循环,您在所有地方都填充了相同的 key/value 对。
根据目的,您可以通过类似的方式获得声明;
Emm = {'name' : 'Emmylou', 'graduate' : 'Fall 2021', 'bill' : 'paid', 'major' : 'Archeology', 'clubs-' : 'Photography, Acting and Glee'}
print(f"Her name is {Emm['name']} and she is on track to graduate in {Emm['graduate']}; Her major is {Emm['major']}; Her clubs - is {Emm['clubs-']}")
您在遍历字典时可能面临的另一个问题是,除非您使用 python 3.7 或更高版本,否则无法保证字典中项目的保存顺序。因此,您的 key/value 对可能不会以相同的顺序出现。
正如其他人告诉您的那样,您正在迭代字典,并且在每次迭代中键和值都被替换并打印在新行中。
如果想使用字典单行打印,可以尝试将字典数组转换成数组,使用format方法打印。
Emm = {
'name' : 'Emmylou',
'graduate' : 'Fall 2021',
'bill' : 'paid',
'major' : 'Archeology',
'clubs-' : 'Photography, Acting and Glee'
}
items = []
for (key, value) in Emm.items():
items = items + [key, value]
print("Her {} is {} and she is on track to {} in {}; Her {} is {}; Her {} is {}; She belongs to these school {} {}".format(*items))
我是 python 和这个论坛的新手。在线学习对我不起作用,所以我不能只去找家教。这可能是我忘记的小事。我欢迎你能给我的任何帮助。
我试图让输出看起来像这样:她的名字是 Emmylou;她有望在 2021 年秋季毕业;她的账单已经付清;她的专业是考古学;她属于这些学校俱乐部——摄影、表演和欢乐合唱团
Emm = {'name' : 'Emmylou', 'graduate' : 'Fall 2021', 'bill' : 'paid', 'major' : 'Archeology', 'clubs-' : 'Photography, Acting and Glee'}
for Key, Value in Emm.items():
print(f"Her {Key} is {Value} and she is on track to {Key} in {Value}; Her {Key} is {Value}; Her {Key} is {Value}; She belongs to these school {Key} {Value}")
输出一团糟,当我 运行 时看起来像这样:
Her name is Emmylou and she is on track to name in Emmylou; Her name is Emmylou; Her name is Emmylou; She belongs to these school name Emmylou
Her graduate is Fall 2021 and she is on track to graduate in Fall 2021; Her graduate is Fall 2021; Her graduate is Fall 2021; She belongs to these school graduate Fall 2021
Her bill is paid and she is on track to bill in paid; Her bill is paid; Her bill is paid; She belongs to these school bill paid
Her major is Archeology and she is on track to major in Archeology; Her major is Archeology; Her major is Archeology; She belongs to these school major Archeology
Her clubs- is Photography, Acting and Glee and she is on track to clubs- in Photography, Acting and Glee; Her clubs- is Photography, Acting and Glee; Her clubs- is Photography, Acting and Glee; She belongs to these school clubs- Photography, Acting and Glee
在您的代码中,您将迭代数据中的每个 key-value 对;所以你最终打印了 5 次,每次都是 key-values 对,而不是打印 1 次,所有 key-value 对。
试试这个。
Emm = [
('name', 'Emmylou'),
('graduate', 'Fall 2021'),
('bill', 'paid'),
('major', 'Archeology'),
('clubs-', 'Photography, Acting and Glee'),
]
flat_items = [item for pair in Emm for item in pair]
print("Her {} is {} and she is on track to {} in {}; Her {} is {}; Her {} is {}; She belongs to these school {} {}".format(*flat_items))
首先,我假设您实际上已经在代码中缩进了 print 语句,否则它根本无法工作。
问题是对于每个循环,您在所有地方都填充了相同的 key/value 对。
根据目的,您可以通过类似的方式获得声明;
Emm = {'name' : 'Emmylou', 'graduate' : 'Fall 2021', 'bill' : 'paid', 'major' : 'Archeology', 'clubs-' : 'Photography, Acting and Glee'}
print(f"Her name is {Emm['name']} and she is on track to graduate in {Emm['graduate']}; Her major is {Emm['major']}; Her clubs - is {Emm['clubs-']}")
您在遍历字典时可能面临的另一个问题是,除非您使用 python 3.7 或更高版本,否则无法保证字典中项目的保存顺序。因此,您的 key/value 对可能不会以相同的顺序出现。
正如其他人告诉您的那样,您正在迭代字典,并且在每次迭代中键和值都被替换并打印在新行中。
如果想使用字典单行打印,可以尝试将字典数组转换成数组,使用format方法打印。
Emm = {
'name' : 'Emmylou',
'graduate' : 'Fall 2021',
'bill' : 'paid',
'major' : 'Archeology',
'clubs-' : 'Photography, Acting and Glee'
}
items = []
for (key, value) in Emm.items():
items = items + [key, value]
print("Her {} is {} and she is on track to {} in {}; Her {} is {}; Her {} is {}; She belongs to these school {} {}".format(*items))