air = [int(x) for x in air if x.isdigit()] ^^^^^^^^^ AttributeError: 'float' object has no attribute 'isdigit'
时间: 2023-11-13 14:11:38 浏览: 151
这个错误通常是由于代码中使用了isdigit()方法,而该方法只能用于字符串类型,不能用于浮点数类型。在这个错误的代码中,air是一个列表,它的元素可能是字符串或浮点数类型,如果元素是浮点数类型,就不能调用isdigit()方法。
要解决这个问题,可以使用Python内置的isinstance()函数判断元素类型,避免对浮点数类型使用isdigit()方法。修改代码如下:
```
air = [int(x) for x in air if isinstance(x, str) and x.isdigit()]
```
这样就可以避免对浮点数类型使用isdigit()方法,只对字符串类型的元素进行转换。
相关问题
if row_list[4].isdigit(): ^^^^^^^^^^^^^^^^^^^ AttributeError: 'numpy.ndarray' object has no attribute 'isdigit'
这个错误是因为numpy.ndarray对象没有isdigit()方法,所以不能在其上调用该方法。isdigit()方法是字符串对象的方法,用于检查字符串是否只包含数字字符。因此,您需要将numpy.ndarray对象转换为字符串对象,然后再调用isdigit()方法。您可以使用astype()方法将numpy.ndarray对象转换为字符串对象,如下所示:
```python
if str(row_list[4]).isdigit():
```
这将把row_list转换为字符串对象,然后再调用isdigit()方法来检查它是否只包含数字字符。
s = "abc123def456"nums = ""for c in s: if c.isdigit(): nums += cresult = int(nums)print(result)用c语言
把Python代码转换为C语言代码如下:
#include <stdio.h>
#include <string.h>
int main() {
char s[] = "abc123def456";
char nums[100] = "";
int result = 0;
int i;
for (i = 0; i < strlen(s); i++) {
if (isdigit(s[i])) {
strncat(nums, &s[i], 1);
}
}
result = atoi(nums);
printf("%d", result);
return 0;
}
阅读全文
相关推荐
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)