【问题标题】:Indexing in a for loopfor循环中的索引
【发布时间】:2021-07-08 06:23:01
【问题描述】:

我正在尝试获取字典中最左边的数字和非数字列表的索引。 我在下面尝试了以下操作,但在第一个 for 循环中它没有给我正确的索引,第二个 for 循环似乎没有为 ind_num 变量分配任何东西。 我的错误在哪里?

感谢您的帮助!

dic = {"hello": 1, "column": [[1, float], [3, str], [4, int], [2, str]]}

columns = dic["column"]
for col in columns:
    if col[1] != [float, int]:
        ind_non = columns.index(col)  # getting the index for the first non-numerical column
        break

for col in columns:
    if col[1] == [float, int]:
        ind_num = columns.index(col)  # getting the index for the first numerical column
        break

print(ind_non)
print(ind_num)

【问题讨论】:

  • if col[1] != [float, int]: 用你自己的话来说,对于给定的输入,你期望col[1] 有什么值?当您将这些与[float, int] 进行比较时,您希望应用什么逻辑?例如,您认为int != [float, int] 的结果应该是什么?为什么?另外,您想通过存储这种数据来解决什么问题[3, str] 这样的值对您和您的程序实际上意味着是什么?
  • 你想在 ind_non 和 num 中显示什么? [1,float] 这里是 1 还是这个列表在列中的索引?
  • @Neeraj 列中列表的索引
  • @KarlKnechtel 我希望int == [float, int] 给我 TRUE。我希望 col[1] 访问列表的第二个值,即数据类型。目标是获取列中列表的索引,以便以后可以将此索引用于其他操作。
  • “我希望 int == [float, int] 给我 TRUE。”好吧,但是为什么?用你自己的话来说,== 是什么意思?

标签: python list dictionary indexing boolean


【解决方案1】:

这些是一般要记住的要点:

  1. 始终声明并设置您将在条件语句中使用的默认变量。 IE。在你的代码中ind_nonind_num 如果元素不存在于列表'column'中会抛出一个错误。

  2. col[1] == [float, int] 错了,你在比较int == [float, int]。而是检查 int 是否存在于 [float, int] 中,就像我在下面提到的那样。

     dic = {"hello": 1, "column": [[1, float], [3, str], [4, int], [2, str]]}
    
     columns = dic["column"]
     ind_non = None
     ind_num = None
     for col in columns:
         if col[1] not in [float, int]:
             ind_non = columns.index(col)  # getting the index for the first non-numerical column
             break
    
    
     for col in columns:
         if col[1] in [float, int]:
             ind_num = columns.index(col)  # getting the index for the first numerical column
             break
    
     print(ind_non)
     print(ind_num)
     # 1
     # 0
    

【讨论】:

    【解决方案2】:

    ==!=检查左右两边的值是否相等:

    if col[1] != [float, int]:
    

    假设您知道col[1] 包含诸如str 之类的值,您可能想检查该值是否不在运算符右侧的列表中。

    要以您的风格做我刚才所说的,只需使用not inin 关键字:

    dic = {"hello": 1, "column": [[1, float], [3, str], [4, int], [2, str]]}
    
    columns = dic["column"]
    for col in columns:
        if col[1] not in [float, int]:
            ind_non = columns.index(col)  # getting the index for the first non-numerical column
            break
    
    for col in columns:
        if col[1] in [float, int]:
            ind_num = columns.index(col)  # getting the index for the first numerical column
            break
    
    print(ind_non)
    print(ind_num)
    

    【讨论】:

      【解决方案3】:

      将第一个if 条件更改为:

      if col[1] != float and col[1] != int: #Will be true for non-numerical data type
      

      将第二个if 条件更改为:

      if col[1] != str:                     #Will be true for numerical data type 
      

      【讨论】:

        【解决方案4】:
        col[1] != [float, int]:
        

        这样做是错误的。您可以使用or 关键字或any 编写它。

        dic = {"hello": 1, "column": [[1, float], [3, str], [4, int], [2, str]]}
        
        columns = dic["column"]
        for col in columns:
            if col[1] != float and col[1] != int:
                ind_non = columns.index(col)  # getting the index for the first non-numerical column
                break
        
        for col in columns:
            if col[1] != str:
                ind_num = columns.index(col)  # getting the index for the first numerical column
                break
        
        print(ind_non)
        print(ind_num)
        

        【讨论】:

          猜你喜欢
          • 2020-09-09
          • 1970-01-01
          • 2012-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-06-11
          • 2011-12-07
          相关资源
          最近更新 更多