Python TypeError: ‘tuple’ object is not callable when using list comprehension inside for loop

huangapple go评论85阅读模式
英文:

Python TypeError: 'tuple' object is not callable when using list comprehension inside for loop

问题

我尝试使用列表理解生成一个字符串列表,其中字符串使用f-strings格式化:

features = [("month", (1, 12)), ("day_of_year", (1, 365))]
for feature, range in features:
  cols = [f"transformed_{feature}_period_{i:02d}" for i in range(12)]

如果运行此代码,会出现错误:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 3
      1 features = [("month", (1, 12)), ("day_of_year", (1, 365))]
      2 for feature, range in features:
----> 3     cols= [f"transformed_{feature}_period_{i:02d}" for i in range(12)]

TypeError: 'tuple' object is not callable

即使从字符串中删除变量,我仍然会得到错误:

for feature, range in features:
  cols = ["transformed_feature_period" for i in range(12)]

我不明白为什么解释器会抱怨我试图调用一个元组。我能够在外部for循环中执行其他操作,比如实际的函数调用和断言,但在尝试使用列表理解时一直遇到这个错误。

提前感谢任何帮助。

英文:

I'm trying to generate a list of strings using a list comprehension, where the string is formatted using f-strings:

features = [("month", (1, 12)), ("day_of_year", (1, 365))]
for feature, range in features:
  cols= [f"transformed_{feature}_period_{i:02d}" for i in range(12)]

If I run this code, I get an error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Cell In[4], line 3
      1 features = [("month", (1, 12)), ("day_of_year", (1, 365))]
      2 for feature, range in features:
----> 3     cols= [f"transformed_{feature}_period_{i:02d}" for i in range(12)]

TypeError: 'tuple' object is not callable

I still get the error even if I remove the variables from the string:

for feature, range in features:
  cols= ["transformed_feature_period" for i in range(12)]

I don't understand why the interpreter is complaining that I'm trying to call a tuple. I'm able to perform other operations, such as actual function calling and asserts inside the outer for loop, but I keep bumping into this error when trying to use a list comprehension.

Thanks in advance for any help.

答案1

得分: 2

If you don't actually use the range,

for feature, _ in features:
    ...

If you only care about the endpoints of the range,

for feature, (start, stop) in features:
    ...

If you really need a reference to the tuple as a whole, pick a name that doesn't shadow the built-in type you use later:

for feature, feature_range in features:
    ...

I don't recommend using builtins.range to access the shadowed type when it's simpler to provide a more accurate name for the index variable in the first place.

英文:

If you don't actually use the range,

for feature, _ in features:
    ...

If you only care about the endpoints of the range,

for feature, (start, stop) in features:
    ...

If you really need a reference to the tuple as a whole, pick a name that doesn't shadow the built-in type you use later:

for feature, feature_range in features:
    ...

I don't recommend using builtins.range to access the shadowed type when it's simpler to provide a more accurate name for the index variable in the first place.

答案2

得分: 1

Make sure you change your Jupyter notebook or any other idle you are using because the range() function will become tuple as it will override the built-in range.

features = [("month", (1, 12)), ("day_of_year", (1, 365))]
for feature, r in features:
    print(feature, range(int(r[0]), int(r[1])))

# Output

month range(1, 12)
day_of_year range(1, 365)

So, this will be the final code:

features = [("month", (1, 12)), ("day_of_year", (1, 365))]
for feature, r in features:
  cols = [f"transformed_{feature}_period_{i:02d}" for i in range(int(r[0]), int(r[1]))]

print(cols)
['transformed_day_of_year_period_01',
 'transformed_day_of_year_period_02',
 'transformed_day_of_year_period_03',
 'transformed_day_of_year_period_04',
 'transformed_day_of_year_period_05',
 ...
 'transformed_day_of_year_period_364']
英文:

Make sure you change your Jupyter notebook or anyother idle you are using because the range() function will become tuple as it will override the buitin range .

features = [("month", (1, 12)), ("day_of_year", (1, 365))]
for feature, r in features:
    print(feature, range(int(r[0]),int(r[1])))

#output

month range(1, 12)
day_of_year range(1, 365)

So, this will be the final code:

features = [("month", (1, 12)), ("day_of_year", (1, 365))]
for feature, r in features:
  cols= [f"transformed_{feature}_period_{i:02d}" for i in range(int(r[0]),int(r[1]))]

print(cols)
['transformed_day_of_year_period_01',
 'transformed_day_of_year_period_02',
 'transformed_day_of_year_period_03',
 'transformed_day_of_year_period_04',
 'transformed_day_of_year_period_05',
  ....
 'transformed_day_of_year_period_364']

huangapple
  • 本文由 发表于 2023年3月9日 21:37:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/75685364.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定