0
我想创建一个基于逻辑条件集合的目录,但它只能通过第二个逻辑语句正确执行。有人能解释为什么python字典是以这种方式行事吗?
# Sample code:
test_dict = {}
file_0 = "C:/Year/yea_84.txt"
file_1 = "C:/Year/yea_92.txt"
file_2 = "C:/Year/yea_01.txt"
file_3 = "C:/Year/yea_06.txt"
for x in range(1985, 2008):
if (x <= 1991):
test_dict[x] = file_0
elif (x > 1991 & x <= 2000):
test_dict[x] = file_1
elif (x > 2000 & x <= 2005):
test_dict[x] = file_2
elif (x > 2005):
test_dict[x] = file_3
print test_dict
# Print result
1985 C:/Year/yea_84.txt
1986 C:/Year/yea_84.txt
1987 C:/Year/yea_84.txt
1988 C:/Year/yea_84.txt
1989 C:/Year/yea_84.txt
1990 C:/Year/yea_84.txt
1991 C:/Year/yea_84.txt
1992 C:/Year/yea_92.txt
1993 C:/Year/yea_92.txt
1994 C:/Year/yea_92.txt
1995 C:/Year/yea_92.txt
1996 C:/Year/yea_92.txt
1997 C:/Year/yea_92.txt
1998 C:/Year/yea_92.txt
1999 C:/Year/yea_92.txt
2000 C:/Year/yea_92.txt
2001 C:/Year/yea_92.txt
2002 C:/Year/yea_92.txt
2003 C:/Year/yea_92.txt
2004 C:/Year/yea_92.txt
2005 C:/Year/yea_92.txt
2006 C:/Year/yea_92.txt
2007 C:/Year/yea_92.txt
我怀疑这是因为在每个循环字典洗牌顺序,但这似乎是一个不好的解释。有人可以扩展这种行为吗?
感谢您详细阐述了比较链接! – cptpython