Python字典输出问题

Python字典输出问题,python,dictionary,mergeddictionaries,Python,Dictionary,Mergeddictionaries,我不熟悉python和这个论坛。在线学习对我不起作用,所以我不能只去找家教。可能是我忘记了一些小事。我欢迎你给我任何帮助 我试图让输出看起来像这样:她的名字是埃米洛;她有望在2021年秋季毕业;她的账单付清了;她的专业是考古学;她属于这些学校俱乐部——摄影、表演和欢乐合唱团 Emm = {'name' : 'Emmylou', 'graduate' : 'Fall 2021', 'bill' : 'paid', 'major' : 'Archeology', 'clubs-' : 'Photog

我不熟悉python和这个论坛。在线学习对我不起作用,所以我不能只去找家教。可能是我忘记了一些小事。我欢迎你给我任何帮助

我试图让输出看起来像这样:她的名字是埃米洛;她有望在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

在代码中,迭代数据中的每个键值对;因此,您最终打印了5次,每次使用一个键值对,而不是使用所有键值对打印1次

试试这个

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语句,因为否则它根本不起作用

问题是,对于每个循环,您在所有位置填充相同的键/值对

根据目的,你可以通过以下方式获得陈述:

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或更高版本,否则无法保证在字典中保存项目的顺序。因此,您的键/值对可能不会以它们进入的相同顺序出现。

正如其他人告诉您的那样,您正在迭代字典,在每次迭代中,键和值都会被替换并打印在新的行中

如果要使用字典在单行中打印,可以尝试将字典转换为数组并使用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))

因为您知道应该在句子中放置哪个键,所以您可以只执行Emm['key']来获取值,而不是在字典中进行迭代。例如:print(f)“她的名字是Emm['name'],她即将毕业于Emm['graduate']..字典在概念上没有顺序。由于变量的大小写敏感性,您提供的解决方案代码将遇到错误,在print语句中使用了“emm”,但emm中的e需要大写,其次print语句将打印5次,因为这是字典的长度。谢谢@BrendaMejia,我已经根据需要编辑了代码。这里唯一的问题是,对于许多版本的Python,字典键的迭代顺序无法保证。哦,我使用新版本太久了,以至于忘记了这一点。看起来OrderDict可能有点过火,使用元组列表进行更新。这里唯一的问题是,对于许多版本的Python来说Python的迭代不能保证字典键的迭代顺序。