Python for循环中的索引

Python for循环中的索引,python,list,dictionary,indexing,boolean,Python,List,Dictionary,Indexing,Boolean,我试图在字典中得到最左边的数字和非数字列表的索引。 我尝试了下面的方法,但是在第一个for循环中,它没有给我正确的索引,第二个for循环似乎没有给ind_num变量赋值。 我的错误在哪里 谢谢你的帮助 dic = {"hello": 1, "column": [[1, float], [3, str], [4, int], [2, str]]} columns = dic["column"] for col in columns:

我试图在字典中得到最左边的数字和非数字列表的索引。 我尝试了下面的方法,但是在第一个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)
这样做是错误的。您可以使用
关键字或
任何
来编写它

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)

将第一个
if
条件更改为:

if col[1] != float and col[1] != int: #Will be true for non-numerical data type
if col[1] != str:                     #Will be true for numerical data type 
将第二个
if
条件更改为:

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

一般来说,需要记住以下几点:

  • 始终声明并设置将在条件语句中使用的默认变量。例如,如果列表“列”中不存在元素,则代码中的
    ind_non
    ind_num
    将抛出错误

  • col[1]==[float,int]
    错误,您正在比较
    int==[float,int]
    。相反,检查[float,int]中是否存在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
    

  • ==
    =检查左右侧值是否相等:

    if col[1] != [float, int]:
    
    假设您知道
    col[1]
    包含一个值,如
    str
    ,您可能想检查该值是否不在运算符右侧的列表中

    要以您的风格完成我刚才所说的内容,只需使用
    notin
    in
    关键字:

    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)
    

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