如果輸入資料是一個串列,它可以正常作業
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}")
當然一定是錯誤
如何處理字典資料?(它們都不是我的實際代碼,這只是一個示例)
uj5u.com熱心網友回復:
您可以在 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
請注意,您無法通過位置索引訪問字典,因此輸出的實際用途有限
uj5u.com熱心網友回復:
在.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
uj5u.com熱心網友回復:
這是我對問題的解決方案。我去掉了大部分多余的代碼。
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/322302.html
下一篇:我想要串列中每個名字的數量
