在 for 循环中建立索引
Indexing in a for loop
我正在尝试获取字典中最左边的数字和非数字列表的索引。
我在下面尝试了以下操作,但在第一个 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)
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)
将第一个 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
这些是一般要记住的要点:
始终声明并设置您将在条件语句中使用的默认变量。 IE。如果元素不在列表 'column'.
中,在您的代码 ind_non
和 ind_num
中会抛出错误
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
我正在尝试获取字典中最左边的数字和非数字列表的索引。 我在下面尝试了以下操作,但在第一个 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)
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)
将第一个 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
这些是一般要记住的要点:
始终声明并设置您将在条件语句中使用的默认变量。 IE。如果元素不在列表 'column'.
中,在您的代码ind_non
和ind_num
中会抛出错误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