[解决]TypeError: unsupported operand type(s) for xx && AttributeError: module xx have no attribute xx

【python常见错误】 不同类型之间的运算&&关键字或模块名命名文件

  • TypeError: unsupported operand type(s) for -: ‘list’ and 'int’
# flatten函数:可以将高维数组转化为向量,生成一份新的复制
a = arange([[n + m * 10 for n in range(3)] for m in range(4)])
print(a)
b = a.flatten()
print(b)

运行之后出现如下错误:
图片1
很明显可以看到错误原因,就是list和int类型之间的“-”运算错误。
然后改了一下上面的写法就对了(这里我好像没怎么理解,我看里面好像没有‘-’呀,刚刚学python,以后理解了来补充)

# flatten函数:可以将高维数组转化为向量,生成一份新的复制
a = array([[n+m*10 for n in range(3)] for m in range(4)])
print(a)
b = a.flatten()
print(b)

运行结果如下:(前面的a是一个4行3列的二维数组,flatten函数将其转换为向量)
图片2

  • AttributeError: module ‘pandas’ has no attribute 'Series’
    我的代码是:
import pandas as pd
s = pd.Series([1, 2, 3])
print(s)

在这里插入图片描述
这个错误刚开始真的有点疑惑,我明明下载了pandas模块,这个模块里面也有Series方法啊,真的疑惑,后来发现是文件名的问题,我的文件命名成 pandas.py 了(如上图的"e:\Python\code\pandas.py"),所以不要用关键字、包名命名文件名,否则导入包和文件的时候,会发生混淆。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 根据你提供的引用内容,报错信息是TypeError: unsupported operand type(s) for +: 'module' and 'module'。这个错误通常发生在使用+运算符时,操作数的类型不兼容。根据引用\[1\]和引用\[2\]的描述,这个错误可能是由于使用了不支持+运算符的类型进行相加操作。而根据引用\[3\]的解释,这个错误可能是由于操作数中包含了函数或方法对象,而不是数值或字符类型。要解决这个问题,你需要检查你的代码,确保你使用+运算符的操作数是兼容的类型,例如数值或字符类型。如果你确定操作数的类型是正确的,那么可能是你的代码中存在其他问题导致了这个错误。你可以进一步检查你的代码,查找其他可能的错误。 #### 引用[.reference_title] - *1* *2* [Python解决报错:TypeError: unsupported operand type(s) for /: ‘builtin_function_or_method‘ and ...](https://blog.csdn.net/weixin_53606817/article/details/129743706)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [Python报错记录 | TypeError: unsupported operand type(s) for +: ‘bool‘ and ‘str](https://blog.csdn.net/Dollilo/article/details/128446753)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值