如果输入数据是列表,它工作正常
list_data = [7,10,9,20,2,5,15,6,25,2,8,17,23,9,24,45,3,22,6,20,4]
pos_1 = 0
pos_2 = 0
for i in range(len(list_data)):
if list_data[i] >= 25:
pos_1 = i
break
print(f"Position1 which has value greater than equal to 25: {pos_1}")
for i in range(pos_1+1,len(list_data)):
if list_data[i] >= 25:
pos_2 = i
break
print(f"Position2 which has value greater than equal to 25: {pos_2}")
输出(第二个for循环不会重复第一个for循环)
Position1 which has value greater than equal to 25: 8
Position2 which has value greater than equal to 25: 15
但是如果数据是字典这样的
dic_data = {1: 10, 3: 20, 6: 15, 8: 25, 11: 17, 12: 23, 14: 24, 15: 45, 17: 22, 19: 20}
index_1 = 0
for i in range(len(dic_data)):
if(dic_data.values() >= 25):
pos_1 = dic_data.keys()
index_1 = i
break
print(f"Position1 which has value greater than equal to 25: {pos_1}")
for i in range(index_1+1,len(dic_data)):
if(dic_data.values() >= 25):
pos_2 = dic_data.keys()
break
print(f"Position2 which has value greater than equal to 25: {pos_2}")
当然是错误
如何处理字典数据?(这两个都不是我的实际代码,它只是一个例子)
可以将迭代器变量与enumerate()函数一起使用:
dic_data = {1: 10, 3: 20, 6: 15, 8: 25, 11: 17, 12: 23,
14: 24, 15: 45, 17: 22, 19: 20}
iData = enumerate(dic_data.values()) # or iter(dic_data.items()) to get keys
for i,value in iData:
if (value >= 25):
pos_1 = i
break
print(f"Position1 which has value greater than equal to 25: {pos_1}")
for i,value in iData:
if(value >= 25):
pos_2 = i
break
print(f"Position2 which has value greater than equal to 25: {pos_2}")
输出:
Position1 which has value greater than equal to 25: 3
Position2 which has value greater than equal to 25: 7
请注意,不能通过位置索引访问字典,因此输出的实际用途有限
.keys()
和.values()
表示键或值的整个列表,您不能对其进行比较或索引。你需要迭代dict的对:items
dic_data = {1: 10, 3: 20, 6: 15, 8: 25, 11: 17, 12: 23, 14: 24, 15: 45, 17: 22, 19: 20}
index_1, pos_1, pos_2 = 0, 0, 0
for idx, (k, v) in enumerate(dic_data.items()):
if v >= 25:
pos_1 = k
index_1 = idx
break
print(f"Position1 which has value greater than equal to 25: {pos_1}")
for idx, (k, v) in enumerate(list(dic_data.items())[index_1 + 1:]):
if v >= 25:
pos_2 = k
break
print(f"Position2 which has value greater than equal to 25: {pos_2}")
Position1 which has value greater than equal to 25: 8
Position2 which has value greater than equal to 25: 15
这是我对这个问题的解决方案。我去掉了大部分多余的代码。
ps:我添加了一个变量,显示位置(first_ value
和second_value
)的值,以防您想检查答案的有效性。
dic_data = {1: 10, 3: 20, 6: 15, 8: 25, 11: 17, 12: 23, 14: 24, 15: 45, 17: 22, 19: 20}
extra_list = list(dic_data.values())
for i in extra_list:
if(i >= 25):
pos_1 = extra_list.index(i)
first_value = i
break
print(f"Position1 which has value greater than equal to 25: {pos_1}.The value in that position is {first_value}")
for i in extra_list[pos_1+1:]:
if(i >= 25):
pos_2 = extra_list.index(i)
second_value = i
break
print(f"Position2 which has value greater than equal to 25: {pos_2}. The value in that position is {second_value}.")
我创建了一个应该有助于节省时间的函数
def greater_equal_25(list1, pos = 0):
'''based on your needs use the value or key methods on the dictionary and then
create a variable that has the list of the method using the list function'''
if pos == 0:
for i in list1[pos:]:
if (i >= 25):
posx = extra_list.index(i)
second_value = i
break
else:
for i in list1[pos+1:]:
if (i >= 25):
posx= extra_list.index(i)
second_value = i
break
return posx