Python 缩短在字典中向下打印单个值
我刚刚为python编写了这段简短的代码。这是学校的时间表。我不经常使用字典,因为这是我经常感到困惑的部分 我想要的代码是Python 缩短在字典中向下打印单个值,python,dictionary,Python,Dictionary,我刚刚为python编写了这段简短的代码。这是学校的时间表。我不经常使用字典,因为这是我经常感到困惑的部分 我想要的代码是打印(星期一[“第1时段]),我重复了5次,需要清理,因此只需要一行代码 我在想也许我应该使用for循环。但由于我并不真正喜欢for循环,所以我不知道如何正确使用它们。除了一两次 这是我迄今为止完成的代码 monday = {"P1" : "1 - English", "P2" : "2 - Maths", "P3" : "3 - PE",
打印(星期一[“第1时段])
,我重复了5次,需要清理,因此只需要一行代码
我在想也许我应该使用for循环。但由于我并不真正喜欢for循环,所以我不知道如何正确使用它们。除了一两次
这是我迄今为止完成的代码
monday = {"P1" : "1 - English",
"P2" : "2 - Maths",
"P3" : "3 - PE",
"P4" : "4 - Computing",
"P5" : "5 - Computing"}
choice_day = input("What day would you like to know what you have? ")
choice_period = input("What period? Or just type NO if you want to know the full day: ")
if choice_day == "monday" and choice_period == "NO":
print(monday["P1"])
print(monday["P2"])
print(monday["P3"])
print(monday["P4"])
print(monday["P5"])
字典中所有值的列表显示为
monday.values()
或者,您可以将列表中的每个值放入一个由换行符连接的字符串中:
if choice_day == "monday" and choice_period == "NO":
print '\n'.join(monday.values())
如果它们应该按顺序排列,请使用排序:
if choice_day == "monday" and choice_period == "NO":
print '\n'.join(sorted(monday.values()))
dict havevalues()
方法,该方法返回给定字典中所有可用值的列表。
您可以简单地迭代该列表,然后尝试以下代码:
if choice_day == "monday" and choice_period == "NO":
for v in monday.values():
print v
假设您只想打印键,然后使用keys()
方法而不是values()
如果要打印这两个键,请使用items()
:
if choice_day == "monday" and choice_period == "NO":
for k, v in monday.items():
print k, v
假设您希望按照示例中按字母顺序排序的键打印值,您可以使用以下方法:
if choice_day == "monday" and choice_period == "NO":
print '\n'.join(monday[k] for k in sorted(monday))
order = ["P1", "P2", "P3", "P4", "P5"]
if choice_day == "monday" and choice_period == "NO":
print '\n'.join(monday[k] for k in order)
如果在实际代码中,键的排序方式与字母排序方式不同,并且您事先知道这种排序方式,则可以执行以下操作:
if choice_day == "monday" and choice_period == "NO":
print '\n'.join(monday[k] for k in sorted(monday))
order = ["P1", "P2", "P3", "P4", "P5"]
if choice_day == "monday" and choice_period == "NO":
print '\n'.join(monday[k] for k in order)
你肯定想把课程安排得井井有条dict
值本质上没有顺序。因此,在这里使用列表
可能比使用dict
更合适
monday = ["1 - English",
"2 - Maths",
"3 - PE",
"4 - Computing",
"5 - Computing"]
if choice_day == "monday" and choice_period == "NO":
for course in monday:
print(course)
通过不使用dict,您可以避免排序,或者使用声明句点1在句点2之前的列表,等等(例如,order=['P1','P2',…]
)。该列表内置了订单
如果出于某种原因,您需要访问(比如)周一的第三门课程,因为Python使用基于0的索引,那么您应该编写Monday[2]
但是,您可能希望使用dict来表示整个计划:
schedule = {'monday': ["1 - English",
"2 - Maths",
"3 - PE",
"4 - Computing",
"5 - Computing"]
'tuesday': [...]}
由于用户可以输入一周中的任何一天,因此您可以在此处使用dict
。
如果您总是按顺序访问计划,那么您可能希望使用有序的数据结构,如列表或元组
现在您可以像这样处理choice\u day
的任何值:
if choice_period == "NO":
for course in schedule[choice_day]:
print(course)
(当用户输入一个不存在的选择时,一定要考虑你想如何处理这种情况……一种可能是在输入时检查选择。另一种选择是使用try.。除了这里的。第三种方法——我的偏好——是使用。)
无论如何,使用dict作为日程安排可以避免像以下这样令人麻木的代码:
if choice_day == 'monday':
...
elif choice_day == 'tuesday':
...
谢谢现在有点明白了!:)@user2816683阅读和阅读其他答案包括其他技术我也是Python的学习者,我想知道排序(周一)
?它是按键排序的吗?你的或排序的(星期一.values()
?@Grijesh,排序的会将dict放入一个列表中。如果你将dict放入一个列表中,你只需要得到键:列表(星期一)
给出了['P2',P3',P1',P4',P5']
,相当于星期一.keys()
。它不是真正的铸造,排序()
接受一个iterable并返回一个排序列表。当迭代一个dict时,你会得到键,因此排序(星期一)
与排序(星期一.keys())
@askewchan@F.J现在我明白了,排序(星期一)
=排序(星期一.keys())
相同,但排序(星期一)
更好,因为它需要一个函数调用——因此速度更快。首先,可能字典不是一个很好的选择。正如我所理解的,sorted(周一)
=sorted(周一.keys())
但是sorted(周一)
比sorted(周一.keys())更好
因为它需要一个函数调用。但是为了进一步解决问题,我们需要join()
。那么哪一个更好。'\n.join(sorted(monday.values())
或'\n.join(monday[k]表示k顺序)
我更喜欢你的一个,因为第一个较小。还有其他偏好的原因吗。