【问题标题】:How to make the next 'for' loop continue loop from previous one through dictionary?如何使下一个“for”循环从前一个循环继续循环到字典?
【发布时间】:2021-12-04 21:01:58
【问题描述】:

如果输入数据是列表,则正常工作

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}")

当然一定是错误

如何处理字典数据?(这两个都不是我的实际代码,只是一个例子)

【问题讨论】:

  • 使用字典,关于您的第一个代码,什么代表键和值?如果值高于 25 保留密钥?
  • “当然一定是错误”是什么意思?你还没有告诉我们第二个代码块的结果是什么。我建议您打印出字典的不同条件和功能,以便您可以看到它们的作用。因为我不认为这段代码在做你想做的事。
  • 对我不清楚的问题向大家道歉。

标签: python list dictionary for-loop


【解决方案1】:

.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

【讨论】:

    【解决方案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
    

    请注意,您不能通过位置索引访问字典,因此输出的实际用途有限

    【讨论】:

    • 你说得对,我最初有 dic_data.keys() ,当我将它切换为返回值索引时,我没有意识到不再需要 iter() 。我确定了答案,谢谢。
    【解决方案3】:

    这是我解决问题的方法。我去掉了大部分多余的代码。

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-05
      • 1970-01-01
      • 2021-08-27
      • 1970-01-01
      • 2019-04-15
      • 2014-07-21
      • 1970-01-01
      • 2020-02-20
      相关资源
      最近更新 更多