134 - Shape类-3

本文介绍了一个关于形状类的编程实例,通过将Shape类抽象化,并实现其子类如正方形、长方形和圆形,展示了如何计算这些形状的周长和面积。代码示例使用了Java语言。

134 - Shape类-3

Time Limit: 1000   Memory Limit: 65535
Submit: 429  Solved: 265

Description

修改题目133,将Shape类改为抽象类

Input

正方形类的边长
长方形类的长宽
圆类的半径

Output

正方形的周长、面积
长方形的周长、面积
圆形的周长、面积

Sample Input

1
1 2
2

Sample Output

4.00 1.00
6.00 2.00
12.57 12.57
import java.util.Scanner;
public class Main{
    public static void main(String[] args) 
    {
    	Shape s1,s2,s3;
         Scanner scan = new Scanner(System.in);      
         double length = scan.nextDouble();
         s1 = new Square(length);
         System.out.printf("%.2f ",s1.getPerimeter());
         System.out.printf("%.2f\n",s1.getArea());
 
         length = scan.nextDouble();
         double wide = scan.nextDouble();
         s2 = new Rectangle(length,wide);
         System.out.printf("%.2f ",s2.getPerimeter());
         System.out.printf("%.2f\n",s2.getArea());
 
         double radius = scan.nextDouble();
         s3 = new Circle(radius);
         System.out.printf("%.2f ",s3.getPerimeter());
         System.out.printf("%.2f\n",s3.getArea());
 
         scan.close(); 
    }
}
abstract class Shape
{
	public abstract double getPerimeter();
	public abstract double getArea();
}
class Square extends Shape
{
	double length;
	public Square(double length)
	{
		this.length = length;
	}
	@Override
	public double getPerimeter()
	{
		return 4 * length;
	}
	@Override
	public double getArea()
	{
		return length * length;
	}
}
class Rectangle extends Square
{
	double width;
	public Rectangle(double length,double width)
	{
		super(length);
		this.width = width;
	}
	@Override
    public double getArea() {
        return length*width;
    }
 
    @Override
    public double getPerimeter() {
        return 2*(length+width);
    }
}
class Circle extends Shape
{
	double r;
	public Circle(double r)
	{
		this.r = r;
	}
	@Override
	public double getArea() 
	{
        return Math.PI * r * r;
    }
    @Override
    public double getPerimeter() 
    {
        return 2 * Math.PI * r;
    }
}

 

PS C:\Users\86152> & D:/miniconda/python.exe d:/code/基线预处理.py d:\code\基线预处理.py:11: FutureWarning: DataFrame.fillna with 'method' is deprecated and will raise in a future version. Use obj.ffill() or obj.bfill() instead. df = df.fillna(method='ffill').fillna(method='bfill').fillna(0) 样本 JL240419_025 错误: 'dict' object has no attribute 'shape' 样本 JL240419_026 错误: 'dict' object has no attribute 'shape' 样本 JL240419_027 错误: 'dict' object has no attribute 'shape' 样本 JL240419_028 错误: 'dict' object has no attribute 'shape' 样本 JL240419_029 错误: 'dict' object has no attribute 'shape' 样本 JL240419_030 错误: 'dict' object has no attribute 'shape' 样本 JL240419_031 错误: 'dict' object has no attribute 'shape' 样本 JL240419_032 错误: 'dict' object has no attribute 'shape' 样本 JL240419_033 错误: 'dict' object has no attribute 'shape' 样本 JL240419_034 错误: 'dict' object has no attribute 'shape' 样本 JL240419_035 错误: 'dict' object has no attribute 'shape' 样本 JL240419_036 错误: 'dict' object has no attribute 'shape' 样本 JL240419_037 错误: 'dict' object has no attribute 'shape' 样本 JL240419_038 错误: 'dict' object has no attribute 'shape' 样本 JL240419_039 错误: 'dict' object has no attribute 'shape' 样本 JL240419_040 错误: 'dict' object has no attribute 'shape' 样本 JL240419_041 错误: 'dict' object has no attribute 'shape' 样本 JL240419_042 错误: 'dict' object has no attribute 'shape' 样本 JL240419_043 错误: 'dict' object has no attribute 'shape' 样本 JL240419_044 错误: 'dict' object has no attribute 'shape' 样本 JL240419_045 错误: 'dict' object has no attribute 'shape' 样本 JL240419_046 错误: 'dict' object has no attribute 'shape' 样本 JL240419_047 错误: 'dict' object has no attribute 'shape' 样本 JL240419_048 错误: 'dict' object has no attribute 'shape' 样本 JL240419_049 错误: 'dict' object has no attribute 'shape' 样本 JL240419_050 错误: 'dict' object has no attribute 'shape' 样本 JL240419_051 错误: 'dict' object has no attribute 'shape' 样本 JL240419_052 错误: 'dict' object has no attribute 'shape' 样本 JL240419_053 错误: 'dict' object has no attribute 'shape' 样本 JL240419_054 错误: 'dict' object has no attribute 'shape' 样本 JL240419_055 错误: 'dict' object has no attribute 'shape' 样本 JL240419_056 错误: 'dict' object has no attribute 'shape' 样本 JL240419_057 错误: 'dict' object has no attribute 'shape' 样本 JL240419_058 错误: 'dict' object has no attribute 'shape' 样本 JL240419_059 错误: 'dict' object has no attribute 'shape' 样本 JL240419_060 错误: 'dict' object has no attribute 'shape' 样本 JL240419_061 错误: 'dict' object has no attribute 'shape' 样本 JL240419_062 错误: 'dict' object has no attribute 'shape' 样本 JL240419_063 错误: 'dict' object has no attribute 'shape' 样本 JL240419_064 错误: 'dict' object has no attribute 'shape' 样本 JL240419_065 错误: 'dict' object has no attribute 'shape' 样本 JL240419_066 错误: 'dict' object has no attribute 'shape' 样本 JL240419_067 错误: 'dict' object has no attribute 'shape' 样本 JL240419_068 错误: 'dict' object has no attribute 'shape' 样本 JL240419_069 错误: 'dict' object has no attribute 'shape' 样本 JL240419_070 错误: 'dict' object has no attribute 'shape' 样本 JL240419_071 错误: 'dict' object has no attribute 'shape' 样本 JL240419_072 错误: 'dict' object has no attribute 'shape' 样本 JL240419_073 错误: 'dict' object has no attribute 'shape' 样本 JL240419_074 错误: 'dict' object has no attribute 'shape' 样本 JL240419_075 错误: 'dict' object has no attribute 'shape' 样本 JL240419_076 错误: 'dict' object has no attribute 'shape' 样本 JL240419_077 错误: 'dict' object has no attribute 'shape' 样本 JL240419_078 错误: 'dict' object has no attribute 'shape' 样本 JL240419_079 错误: 'dict' object has no attribute 'shape' 样本 JL240419_080 错误: 'dict' object has no attribute 'shape' 样本 JL240419_081 错误: 'dict' object has no attribute 'shape' 样本 JL240419_082 错误: 'dict' object has no attribute 'shape' 样本 JL240419_083 错误: 'dict' object has no attribute 'shape' 样本 JL240419_084 错误: 'dict' object has no attribute 'shape' 样本 JL240419_085 错误: 'dict' object has no attribute 'shape' 样本 JL240419_086 错误: 'dict' object has no attribute 'shape' 样本 JL240419_087 错误: 'dict' object has no attribute 'shape' 样本 JL240419_088 错误: 'dict' object has no attribute 'shape' 样本 JL240419_089 错误: 'dict' object has no attribute 'shape' 样本 JL240419_090 错误: 'dict' object has no attribute 'shape' 样本 JL240419_091 错误: 'dict' object has no attribute 'shape' 样本 JL240419_092 错误: 'dict' object has no attribute 'shape' 样本 JL240419_093 错误: 'dict' object has no attribute 'shape' 样本 JL240419_094 错误: 'dict' object has no attribute 'shape' 样本 JL240419_095 错误: 'dict' object has no attribute 'shape' 样本 JL240419_096 错误: 'dict' object has no attribute 'shape' 样本 JL240419_145 错误: 'dict' object has no attribute 'shape' 样本 JL240419_146 错误: 'dict' object has no attribute 'shape' 样本 JL240419_147 错误: 'dict' object has no attribute 'shape' 样本 JL240419_148 错误: 'dict' object has no attribute 'shape' 样本 JL240419_149 错误: 'dict' object has no attribute 'shape' 样本 JL240419_150 错误: 'dict' object has no attribute 'shape' 样本 JL240419_151 错误: 'dict' object has no attribute 'shape' 样本 JL240419_152 错误: 'dict' object has no attribute 'shape' 样本 JL240419_153 错误: 'dict' object has no attribute 'shape' 样本 JL240419_154 错误: 'dict' object has no attribute 'shape' 样本 JL240419_155 错误: 'dict' object has no attribute 'shape' 样本 JL240419_156 错误: 'dict' object has no attribute 'shape' 样本 JL240419_157 错误: 'dict' object has no attribute 'shape' 样本 JL240419_158 错误: 'dict' object has no attribute 'shape' 样本 JL240419_159 错误: 'dict' object has no attribute 'shape' 样本 JL240419_160 错误: 'dict' object has no attribute 'shape' 样本 JL240419_161 错误: 'dict' object has no attribute 'shape' 样本 JL240419_162 错误: 'dict' object has no attribute 'shape' 样本 JL240419_163 错误: 'dict' object has no attribute 'shape' 样本 JL240419_164 错误: 'dict' object has no attribute 'shape' 样本 JL240419_165 错误: 'dict' object has no attribute 'shape' 样本 JL240419_166 错误: 'dict' object has no attribute 'shape' 样本 JL240419_167 错误: 'dict' object has no attribute 'shape' 样本 JL240419_168 错误: 'dict' object has no attribute 'shape' 样本 JL240419_169 错误: 'dict' object has no attribute 'shape' 样本 HB240525_016 错误: 'dict' object has no attribute 'shape' 样本 HB240525_017 错误: 'dict' object has no attribute 'shape' 样本 HB240525_018 错误: 'dict' object has no attribute 'shape' 样本 HB240525_019 错误: 'dict' object has no attribute 'shape' 样本 HB240525_020 错误: 'dict' object has no attribute 'shape' 样本 HB240525_021 错误: 'dict' object has no attribute 'shape' 样本 HB240525_022 错误: 'dict' object has no attribute 'shape' 样本 HB240525_023 错误: 'dict' object has no attribute 'shape' 样本 HB240525_024 错误: 'dict' object has no attribute 'shape' 样本 HB240525_025 错误: 'dict' object has no attribute 'shape' 样本 HB240525_026 错误: 'dict' object has no attribute 'shape' 样本 HB240525_027 错误: 'dict' object has no attribute 'shape' 样本 HB240525_028 错误: 'dict' object has no attribute 'shape' 样本 HB240525_029 错误: 'dict' object has no attribute 'shape' 样本 HB240525_030 错误: 'dict' object has no attribute 'shape' 样本 HB240525_031 错误: 'dict' object has no attribute 'shape' 样本 HB240525_032 错误: 'dict' object has no attribute 'shape' 样本 HB240525_033 错误: 'dict' object has no attribute 'shape' 样本 HB240525_034 错误: 'dict' object has no attribute 'shape' 样本 HB240525_035 错误: 'dict' object has no attribute 'shape' 样本 HB240525_036 错误: 'dict' object has no attribute 'shape' 样本 HB240525_037 错误: 'dict' object has no attribute 'shape' 样本 HB240525_038 错误: 'dict' object has no attribute 'shape' 样本 HB240525_039 错误: 'dict' object has no attribute 'shape' 样本 HB240525_040 错误: 'dict' object has no attribute 'shape' 样本 HB240525_041 错误: 'dict' object has no attribute 'shape' 样本 HB240525_042 错误: 'dict' object has no attribute 'shape' 样本 HB240525_043 错误: 'dict' object has no attribute 'shape' 样本 HB240525_044 错误: 'dict' object has no attribute 'shape' 样本 HB240525_045 错误: 'dict' object has no attribute 'shape' 样本 HB240525_046 错误: 'dict' object has no attribute 'shape' 样本 HB240525_047 错误: 'dict' object has no attribute 'shape' 样本 HB240525_048 错误: 'dict' object has no attribute 'shape' 样本 HB240525_049 错误: 'dict' object has no attribute 'shape' 样本 HB240525_050 错误: 'dict' object has no attribute 'shape' 样本 HB240525_051 错误: 'dict' object has no attribute 'shape' 样本 HB240525_052 错误: 'dict' object has no attribute 'shape' 样本 HB240525_053 错误: 'dict' object has no attribute 'shape' 样本 HB240525_054 错误: 'dict' object has no attribute 'shape' 样本 HB240525_055 错误: 'dict' object has no attribute 'shape' 样本 HB240525_056 错误: 'dict' object has no attribute 'shape' 样本 HB240525_057 错误: 'dict' object has no attribute 'shape' 样本 HB240525_058 错误: 'dict' object has no attribute 'shape' 样本 HB240525_059 错误: 'dict' object has no attribute 'shape' 样本 HB240525_060 错误: 'dict' object has no attribute 'shape' 样本 HB240525_061 错误: 'dict' object has no attribute 'shape' 样本 HB240525_062 错误: 'dict' object has no attribute 'shape' 样本 HB240525_063 错误: 'dict' object has no attribute 'shape' 样本 HB240525_064 错误: 'dict' object has no attribute 'shape' 样本 HB240525_065 错误: 'dict' object has no attribute 'shape' 样本 HB240525_066 错误: 'dict' object has no attribute 'shape' 样本 HB240525_067 错误: 'dict' object has no attribute 'shape' 样本 HB240525_068 错误: 'dict' object has no attribute 'shape' 样本 HB240626_016 错误: 'dict' object has no attribute 'shape' 样本 HB240626_017 错误: 'dict' object has no attribute 'shape' 样本 HB240626_018 错误: 'dict' object has no attribute 'shape' 样本 HB240626_019 错误: 'dict' object has no attribute 'shape' 样本 HB240626_020 错误: 'dict' object has no attribute 'shape' 样本 HB240626_021 错误: 'dict' object has no attribute 'shape' 样本 HB240626_022 错误: 'dict' object has no attribute 'shape' 样本 HB240626_023 错误: 'dict' object has no attribute 'shape' 样本 HB240626_024 错误: 'dict' object has no attribute 'shape' 样本 HB240626_025 错误: 'dict' object has no attribute 'shape' 样本 HB240626_026 错误: 'dict' object has no attribute 'shape' 样本 HB240626_027 错误: 'dict' object has no attribute 'shape' 样本 HB240626_028 错误: 'dict' object has no attribute 'shape' 样本 HB240626_029 错误: 'dict' object has no attribute 'shape' 样本 HB240626_030 错误: 'dict' object has no attribute 'shape' 样本 HB240626_031 错误: 'dict' object has no attribute 'shape' 样本 HB240626_032 错误: 'dict' object has no attribute 'shape' 样本 HB240626_033 错误: 'dict' object has no attribute 'shape' 样本 HB240626_034 错误: 'dict' object has no attribute 'shape' 样本 HB240626_035 错误: 'dict' object has no attribute 'shape' 样本 HB240626_036 错误: 'dict' object has no attribute 'shape' 样本 HB240626_037 错误: 'dict' object has no attribute 'shape' 样本 HB240626_038 错误: 'dict' object has no attribute 'shape' 样本 HB240626_039 错误: 'dict' object has no attribute 'shape' 样本 HB240626_040 错误: 'dict' object has no attribute 'shape' 样本 HB240626_041 错误: 'dict' object has no attribute 'shape' 样本 HB240626_042 错误: 'dict' object has no attribute 'shape' 样本 HB240626_043 错误: 'dict' object has no attribute 'shape' 样本 HB240626_044 错误: 'dict' object has no attribute 'shape' 样本 HB240626_045 错误: 'dict' object has no attribute 'shape' 样本 JL240612_011 错误: 'dict' object has no attribute 'shape' 样本 JL240612_012 错误: 'dict' object has no attribute 'shape' 样本 JL240612_013 错误: 'dict' object has no attribute 'shape' 样本 JL240612_014 错误: 'dict' object has no attribute 'shape' 样本 JL240612_015 错误: 'dict' object has no attribute 'shape' 样本 JL240612_016 错误: 'dict' object has no attribute 'shape' 样本 JL240612_017 错误: 'dict' object has no attribute 'shape' 样本 JL240612_018 错误: 'dict' object has no attribute 'shape' 样本 JL240612_019 错误: 'dict' object has no attribute 'shape' 样本 JL240612_020 错误: 'dict' object has no attribute 'shape' 样本 JL240612_021 错误: 'dict' object has no attribute 'shape' 样本 JL240612_022 错误: 'dict' object has no attribute 'shape' 样本 JL240612_023 错误: 'dict' object has no attribute 'shape' 样本 JL240612_024 错误: 'dict' object has no attribute 'shape' 样本 JL240612_025 错误: 'dict' object has no attribute 'shape' 样本 JL240612_026 错误: 'dict' object has no attribute 'shape' 样本 JL240612_027 错误: 'dict' object has no attribute 'shape' 样本 JL240612_028 错误: 'dict' object has no attribute 'shape' 样本 JL240612_029 错误: 'dict' object has no attribute 'shape' 样本 JL240612_030 错误: 'dict' object has no attribute 'shape' 样本 JL240612_031 错误: 'dict' object has no attribute 'shape' 样本 JL240612_032 错误: 'dict' object has no attribute 'shape' 样本 JL240612_033 错误: 'dict' object has no attribute 'shape' 样本 JL240612_034 错误: 'dict' object has no attribute 'shape' 样本 JL240612_035 错误: 'dict' object has no attribute 'shape' 样本 JL240612_036 错误: 'dict' object has no attribute 'shape' 样本 JL240612_037 错误: 'dict' object has no attribute 'shape' 样本 JL240612_038 错误: 'dict' object has no attribute 'shape' 样本 JL240612_039 错误: 'dict' object has no attribute 'shape' 样本 JL240612_040 错误: 'dict' object has no attribute 'shape' 样本 JL240612_041 错误: 'dict' object has no attribute 'shape' 样本 JL240612_042 错误: 'dict' object has no attribute 'shape' 样本 JL240612_043 错误: 'dict' object has no attribute 'shape' 样本 JL240612_044 错误: 'dict' object has no attribute 'shape' 样本 JL240612_045 错误: 'dict' object has no attribute 'shape' 样本 JL240612_046 错误: 'dict' object has no attribute 'shape' 样本 JL240612_047 错误: 'dict' object has no attribute 'shape' 样本 JL240612_048 错误: 'dict' object has no attribute 'shape' 样本 JL240612_049 错误: 'dict' object has no attribute 'shape' 样本 JL240612_050 错误: 'dict' object has no attribute 'shape' 样本 JL240612_051 错误: 'dict' object has no attribute 'shape' 样本 JL240612_052 错误: 'dict' object has no attribute 'shape' 样本 JL240612_053 错误: 'dict' object has no attribute 'shape' 样本 JL240612_054 错误: 'dict' object has no attribute 'shape' 样本 JL240612_055 错误: 'dict' object has no attribute 'shape' 样本 JL240612_056 错误: 'dict' object has no attribute 'shape' 样本 JL240612_057 错误: 'dict' object has no attribute 'shape' 样本 JL240612_058 错误: 'dict' object has no attribute 'shape' 样本 JL240612_059 错误: 'dict' object has no attribute 'shape' 样本 JL240612_060 错误: 'dict' object has no attribute 'shape' 样本 JL240612_061 错误: 'dict' object has no attribute 'shape' 样本 JL240612_062 错误: 'dict' object has no attribute 'shape' 样本 JL240612_063 错误: 'dict' object has no attribute 'shape' 样本 JL240612_064 错误: 'dict' object has no attribute 'shape' 样本 JL240612_065 错误: 'dict' object has no attribute 'shape' 样本 JL240612_066 错误: 'dict' object has no attribute 'shape' 样本 JL240612_067 错误: 'dict' object has no attribute 'shape' 样本 JL240612_068 错误: 'dict' object has no attribute 'shape' 样本 JL240612_069 错误: 'dict' object has no attribute 'shape' 样本 JL240612_070 错误: 'dict' object has no attribute 'shape' 样本 JL240612_071 错误: 'dict' object has no attribute 'shape' 样本 JL240612_072 错误: 'dict' object has no attribute 'shape' 样本 JL240612_073 错误: 'dict' object has no attribute 'shape' 样本 JL240612_074 错误: 'dict' object has no attribute 'shape' 样本 JL240612_075 错误: 'dict' object has no attribute 'shape' 样本 JL240612_076 错误: 'dict' object has no attribute 'shape' 样本 JL240612_077 错误: 'dict' object has no attribute 'shape' 样本 JL240612_078 错误: 'dict' object has no attribute 'shape' 样本 JL240612_079 错误: 'dict' object has no attribute 'shape' 样本 JL240612_080 错误: 'dict' object has no attribute 'shape' 样本 JL240612_081 错误: 'dict' object has no attribute 'shape' 样本 JL240612_082 错误: 'dict' object has no attribute 'shape' 样本 JL240612_083 错误: 'dict' object has no attribute 'shape' 样本 JL240612_084 错误: 'dict' object has no attribute 'shape' 样本 JL240612_085 错误: 'dict' object has no attribute 'shape' 样本 JL240612_086 错误: 'dict' object has no attribute 'shape' 样本 JL240612_087 错误: 'dict' object has no attribute 'shape' 样本 JL240612_088 错误: 'dict' object has no attribute 'shape' 样本 JL240612_089 错误: 'dict' object has no attribute 'shape' 样本 JL240612_090 错误: 'dict' object has no attribute 'shape' 样本 JL240612_091 错误: 'dict' object has no attribute 'shape' 样本 JL240612_092 错误: 'dict' object has no attribute 'shape' 样本 JL240612_093 错误: 'dict' object has no attribute 'shape' 样本 JL240612_094 错误: 'dict' object has no attribute 'shape' 样本 JL240612_095 错误: 'dict' object has no attribute 'shape' 样本 JL240612_096 错误: 'dict' object has no attribute 'shape' 样本 JL240612_097 错误: 'dict' object has no attribute 'shape' 样本 JL240612_098 错误: 'dict' object has no attribute 'shape' 样本 JL240612_099 错误: 'dict' object has no attribute 'shape' 样本 JL240612_100 错误: 'dict' object has no attribute 'shape' 样本 JL240612_101 错误: 'dict' object has no attribute 'shape' 样本 JL240612_103 错误: 'dict' object has no attribute 'shape' 样本 JL240612_104 错误: 'dict' object has no attribute 'shape' 样本 JL240612_105 错误: 'dict' object has no attribute 'shape' 样本 JL240612_106 错误: 'dict' object has no attribute 'shape' 样本 JL240612_107 错误: 'dict' object has no attribute 'shape' 样本 JL240612_108 错误: 'dict' object has no attribute 'shape' 样本 JL240612_109 错误: 'dict' object has no attribute 'shape' 样本 JL240612_110 错误: 'dict' object has no attribute 'shape' 样本 JL240612_111 错误: 'dict' object has no attribute 'shape' 样本 JL240612_112 错误: 'dict' object has no attribute 'shape' 样本 JL240612_113 错误: 'dict' object has no attribute 'shape' 样本 JL240612_114 错误: 'dict' object has no attribute 'shape' 样本 JL240612_115 错误: 'dict' object has no attribute 'shape' 样本 JL240612_116 错误: 'dict' object has no attribute 'shape' 样本 JL240612_117 错误: 'dict' object has no attribute 'shape' 样本 JL240612_118 错误: 'dict' object has no attribute 'shape' 样本 JL240612_119 错误: 'dict' object has no attribute 'shape' 样本 JL240612_120 错误: 'dict' object has no attribute 'shape' 样本 JL240612_121 错误: 'dict' object has no attribute 'shape' 样本 JL240612_122 错误: 'dict' object has no attribute 'shape' 样本 JL240612_123 错误: 'dict' object has no attribute 'shape' 样本 JL240612_124 错误: 'dict' object has no attribute 'shape' 样本 JL240612_125 错误: 'dict' object has no attribute 'shape' 样本 JL240612_126 错误: 'dict' object has no attribute 'shape' 样本 JL240612_127 错误: 'dict' object has no attribute 'shape' 样本 JL240612_128 错误: 'dict' object has no attribute 'shape' 样本 JL240612_129 错误: 'dict' object has no attribute 'shape' 样本 JL240612_130 错误: 'dict' object has no attribute 'shape' 样本 JL240612_131 错误: 'dict' object has no attribute 'shape' 样本 JL240612_132 错误: 'dict' object has no attribute 'shape' 样本 JL240612_133 错误: 'dict' object has no attribute 'shape' 样本 JL240612_134 错误: 'dict' object has no attribute 'shape' 样本 JL240612_135 错误: 'dict' object has no attribute 'shape' 样本 JL240612_136 错误: 'dict' object has no attribute 'shape' 样本 JL240612_137 错误: 'dict' object has no attribute 'shape' 样本 JL240612_138 错误: 'dict' object has no attribute 'shape' 样本 JL240612_139 错误: 'dict' object has no attribute 'shape' 样本 JL240612_140 错误: 'dict' object has no attribute 'shape' 样本 JL240612_141 错误: 'dict' object has no attribute 'shape' 样本 JL240612_142 错误: 'dict' object has no attribute 'shape' 样本 JL240612_143 错误: 'dict' object has no attribute 'shape' 样本 JL240612_144 错误: 'dict' object has no attribute 'shape' 样本 JL240612_145 错误: 'dict' object has no attribute 'shape' 样本 JL240612_146 错误: 'dict' object has no attribute 'shape' 样本 JL240612_147 错误: 'dict' object has no attribute 'shape' 样本 JL240612_148 错误: 'dict' object has no attribute 'shape' 样本 JL240612_149 错误: 'dict' object has no attribute 'shape' 样本 JL240612_150 错误: 'dict' object has no attribute 'shape' 样本 JL240612_151 错误: 'dict' object has no attribute 'shape' 样本 JL240612_152 错误: 'dict' object has no attribute 'shape' 样本 JL240612_153 错误: 'dict' object has no attribute 'shape' 样本 JL240612_154 错误: 'dict' object has no attribute 'shape' 样本 JL240612_155 错误: 'dict' object has no attribute 'shape' 样本 JL240612_156 错误: 'dict' object has no attribute 'shape' 样本 JL240612_157 错误: 'dict' object has no attribute 'shape' 样本 JL240612_158 错误: 'dict' object has no attribute 'shape' 样本 JL240612_159 错误: 'dict' object has no attribute 'shape' 样本 JL240612_160 错误: 'dict' object has no attribute 'shape' 样本 JL240612_161 错误: 'dict' object has no attribute 'shape' 样本 JL240612_162 错误: 'dict' object has no attribute 'shape' 样本 JL240612_163 错误: 'dict' object has no attribute 'shape' 样本 JL240612_164 错误: 'dict' object has no attribute 'shape' 样本 JL240612_165 错误: 'dict' object has no attribute 'shape' 样本 JL240612_166 错误: 'dict' object has no attribute 'shape' 样本 JL240612_167 错误: 'dict' object has no attribute 'shape' 样本 JL240612_168 错误: 'dict' object has no attribute 'shape' 样本 JL240612_169 错误: 'dict' object has no attribute 'shape' 样本 JL240612_170 错误: 'dict' object has no attribute 'shape' 样本 JL240612_171 错误: 'dict' object has no attribute 'shape' 样本 JL240612_172 错误: 'dict' object has no attribute 'shape' 样本 JL240612_173 错误: 'dict' object has no attribute 'shape' 样本 JL240612_174 错误: 'dict' object has no attribute 'shape' 样本 JL240612_175 错误: 'dict' object has no attribute 'shape' 样本 JL240612_176 错误: 'dict' object has no attribute 'shape' 样本 JL240612_177 错误: 'dict' object has no attribute 'shape' 样本 JL240612_178 错误: 'dict' object has no attribute 'shape' 样本 JL240612_179 错误: 'dict' object has no attribute 'shape' 样本 JL240612_180 错误: 'dict' object has no attribute 'shape' 样本 JL240612_181 错误: 'dict' object has no attribute 'shape' 样本 JL240612_182 错误: 'dict' object has no attribute 'shape' 样本 JL240612_183 错误: 'dict' object has no attribute 'shape' 样本 JL240612_184 错误: 'dict' object has no attribute 'shape' 样本 JL240612_185 错误: 'dict' object has no attribute 'shape' 样本 JL240612_186 错误: 'dict' object has no attribute 'shape' 样本 JL240612_187 错误: 'dict' object has no attribute 'shape' 样本 JL240612_188 错误: 'dict' object has no attribute 'shape' 样本 JL240612_189 错误: 'dict' object has no attribute 'shape' 样本 JL240612_190 错误: 'dict' object has no attribute 'shape' 样本 JL240612_191 错误: 'dict' object has no attribute 'shape' 样本 JL240612_192 错误: 'dict' object has no attribute 'shape' 样本 JL240612_193 错误: 'dict' object has no attribute 'shape' 样本 JL240612_194 错误: 'dict' object has no attribute 'shape' 样本 JL240612_195 错误: 'dict' object has no attribute 'shape' 样本 JL240612_196 错误: 'dict' object has no attribute 'shape' 样本 JL240612_197 错误: 'dict' object has no attribute 'shape' 样本 JL240612_198 错误: 'dict' object has no attribute 'shape' 样本 JL240612_199 错误: 'dict' object has no attribute 'shape' 样本 JL240612_200 错误: 'dict' object has no attribute 'shape' 样本 JL240612_201 错误: 'dict' object has no attribute 'shape' 样本 JL240612_202 错误: 'dict' object has no attribute 'shape' 样本 JL240612_203 错误: 'dict' object has no attribute 'shape' 样本 JL240612_204 错误: 'dict' object has no attribute 'shape' 样本 JL240612_205 错误: 'dict' object has no attribute 'shape' 样本 JL240612_206 错误: 'dict' object has no attribute 'shape' 样本 JL240612_207 错误: 'dict' object has no attribute 'shape' 样本 JL240612_208 错误: 'dict' object has no attribute 'shape' 样本 JL240612_209 错误: 'dict' object has no attribute 'shape' 样本 JL240612_210 错误: 'dict' object has no attribute 'shape' 样本 JL240612_211 错误: 'dict' object has no attribute 'shape' 样本 JL240612_212 错误: 'dict' object has no attribute 'shape' 样本 JL240612_213 错误: 'dict' object has no attribute 'shape' 样本 JL240612_214 错误: 'dict' object has no attribute 'shape' 样本 JL240612_215 错误: 'dict' object has no attribute 'shape' 样本 JL240612_216 错误: 'dict' object has no attribute 'shape' 样本 JL240612_217 错误: 'dict' object has no attribute 'shape' 样本 JL240612_218 错误: 'dict' object has no attribute 'shape' 样本 JL240612_219 错误: 'dict' object has no attribute 'shape' 样本 JL240612_220 错误: 'dict' object has no attribute 'shape' 样本 JL240612_221 错误: 'dict' object has no attribute 'shape' 样本 JL240612_222 错误: 'dict' object has no attribute 'shape' 样本 JL240612_223 错误: 'dict' object has no attribute 'shape' 样本 JL240612_224 错误: 'dict' object has no attribute 'shape' 样本 HB240726_001 错误: 'dict' object has no attribute 'shape' 样本 HB240726_002 错误: 'dict' object has no attribute 'shape' 样本 HB240726_003 错误: 'dict' object has no attribute 'shape' 样本 HB240726_004 错误: 'dict' object has no attribute 'shape' 样本 HB240726_005 错误: 'dict' object has no attribute 'shape' 样本 HB240726_006 错误: 'dict' object has no attribute 'shape' 样本 HB240726_007 错误: 'dict' object has no attribute 'shape' 样本 HB240726_008 错误: 'dict' object has no attribute 'shape' 样本 HB240726_009 错误: 'dict' object has no attribute 'shape' 样本 HB240726_010 错误: 'dict' object has no attribute 'shape' 样本 HB240726_011 错误: 'dict' object has no attribute 'shape' 样本 HB240726_012 错误: 'dict' object has no attribute 'shape' 样本 HB240726_013 错误: 'dict' object has no attribute 'shape' 样本 HB240726_014 错误: 'dict' object has no attribute 'shape' 样本 HB240726_015 错误: 'dict' object has no attribute 'shape' 样本 HB240726_016 错误: 'dict' object has no attribute 'shape' 样本 HB240726_017 错误: 'dict' object has no attribute 'shape' 样本 HB240726_018 错误: 'dict' object has no attribute 'shape' 样本 HB240726_019 错误: 'dict' object has no attribute 'shape' 样本 HB240726_020 错误: 'dict' object has no attribute 'shape' 样本 HB240726_021 错误: 'dict' object has no attribute 'shape' 样本 HB240726_022 错误: 'dict' object has no attribute 'shape' 样本 HB240726_023 错误: 'dict' object has no attribute 'shape' 样本 HB240726_024 错误: 'dict' object has no attribute 'shape' 样本 HB240726_025 错误: 'dict' object has no attribute 'shape' 样本 HB240726_026 错误: 'dict' object has no attribute 'shape' 样本 HB240726_028 错误: 'dict' object has no attribute 'shape' 样本 HB240726_030 错误: 'dict' object has no attribute 'shape' 样本 HB240726_031 错误: 'dict' object has no attribute 'shape' 样本 HB240726_032 错误: 'dict' object has no attribute 'shape' 样本 HB240726_033 错误: 'dict' object has no attribute 'shape' 样本 HB240726_034 错误: 'dict' object has no attribute 'shape' 处理完成!结果已保存 d:\code\基线预处理.py:67: UserWarning: Glyph 25289 (\N{CJK UNIFIED IDEOGRAPH-62C9}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 26364 (\N{CJK UNIFIED IDEOGRAPH-66FC}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 20301 (\N{CJK UNIFIED IDEOGRAPH-4F4D}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 31227 (\N{CJK UNIFIED IDEOGRAPH-79FB}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 24378 (\N{CJK UNIFIED IDEOGRAPH-5F3A}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 24230 (\N{CJK UNIFIED IDEOGRAPH-5EA6}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 26679 (\N{CJK UNIFIED IDEOGRAPH-6837}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 26412 (\N{CJK UNIFIED IDEOGRAPH-672C}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 22522 (\N{CJK UNIFIED IDEOGRAPH-57FA}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 26657 (\N{CJK UNIFIED IDEOGRAPH-6821}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 27491 (\N{CJK UNIFIED IDEOGRAPH-6B63}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 21407 (\N{CJK UNIFIED IDEOGRAPH-539F}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 22987 (\N{CJK UNIFIED IDEOGRAPH-59CB}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 20809 (\N{CJK UNIFIED IDEOGRAPH-5149}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 35889 (\N{CJK UNIFIED IDEOGRAPH-8C31}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 25311 (\N{CJK UNIFIED IDEOGRAPH-62DF}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 21512 (\N{CJK UNIFIED IDEOGRAPH-5408}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:67: UserWarning: Glyph 21518 (\N{CJK UNIFIED IDEOGRAPH-540E}) missing from font(s) DejaVu Sans. plt.tight_layout() d:\code\基线预处理.py:68: UserWarning: Glyph 24378 (\N{CJK UNIFIED IDEOGRAPH-5F3A}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 24230 (\N{CJK UNIFIED IDEOGRAPH-5EA6}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 26679 (\N{CJK UNIFIED IDEOGRAPH-6837}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 26412 (\N{CJK UNIFIED IDEOGRAPH-672C}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 22522 (\N{CJK UNIFIED IDEOGRAPH-57FA}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 26657 (\N{CJK UNIFIED IDEOGRAPH-6821}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 27491 (\N{CJK UNIFIED IDEOGRAPH-6B63}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 25289 (\N{CJK UNIFIED IDEOGRAPH-62C9}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 26364 (\N{CJK UNIFIED IDEOGRAPH-66FC}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 20301 (\N{CJK UNIFIED IDEOGRAPH-4F4D}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 31227 (\N{CJK UNIFIED IDEOGRAPH-79FB}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 21407 (\N{CJK UNIFIED IDEOGRAPH-539F}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 22987 (\N{CJK UNIFIED IDEOGRAPH-59CB}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 20809 (\N{CJK UNIFIED IDEOGRAPH-5149}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 35889 (\N{CJK UNIFIED IDEOGRAPH-8C31}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 25311 (\N{CJK UNIFIED IDEOGRAPH-62DF}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 21512 (\N{CJK UNIFIED IDEOGRAPH-5408}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) d:\code\基线预处理.py:68: UserWarning: Glyph 21518 (\N{CJK UNIFIED IDEOGRAPH-540E}) missing from font(s) DejaVu Sans. plt.savefig('raman_correction.png', dpi=300) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 24378 (\N{CJK UNIFIED IDEOGRAPH-5F3A}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 24230 (\N{CJK UNIFIED IDEOGRAPH-5EA6}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 26679 (\N{CJK UNIFIED IDEOGRAPH-6837}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 26412 (\N{CJK UNIFIED IDEOGRAPH-672C}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 30340 (\N{CJK UNIFIED IDEOGRAPH-7684}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 22522 (\N{CJK UNIFIED IDEOGRAPH-57FA}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 32447 (\N{CJK UNIFIED IDEOGRAPH-7EBF}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 26657 (\N{CJK UNIFIED IDEOGRAPH-6821}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 27491 (\N{CJK UNIFIED IDEOGRAPH-6B63}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 25289 (\N{CJK UNIFIED IDEOGRAPH-62C9}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 26364 (\N{CJK UNIFIED IDEOGRAPH-66FC}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 20301 (\N{CJK UNIFIED IDEOGRAPH-4F4D}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 31227 (\N{CJK UNIFIED IDEOGRAPH-79FB}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 21407 (\N{CJK UNIFIED IDEOGRAPH-539F}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 22987 (\N{CJK UNIFIED IDEOGRAPH-59CB}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 20809 (\N{CJK UNIFIED IDEOGRAPH-5149}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 35889 (\N{CJK UNIFIED IDEOGRAPH-8C31}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 25311 (\N{CJK UNIFIED IDEOGRAPH-62DF}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 21512 (\N{CJK UNIFIED IDEOGRAPH-5408}) missing from font(s) DejaVu Sans. func(*args) D:\miniconda\Lib\tkinter\__init__.py:862: UserWarning: Glyph 21518 (\N{CJK UNIFIED IDEOGRAPH-540E}) missing from font(s) DejaVu Sans. func(*args) PS C:\Users\86152>
最新发布
12-10
<think>我们遇到了两个主要错误: 1. ValueError: setting an array element with a sequence. 2. AttributeError: &#39;dict&#39; object has no attribute &#39;shape&#39; 同时,还有字体缺失的警告。 根据引用[1]引用[2]的提示,第一个错误通常是由于数组元素赋值时形状不匹配或数据型不一致造成的。第二个错误则是因为尝试访问字典对象的&#39;shape&#39;属性,而字典并没有这个属性。 另外,引用[4]提到了版本兼容性问题,但这里我们主要处理前两个错误。 我们将分别解决这两个错误,并处理字体缺失警告。 步骤: 1. 解决ValueError: setting an array element with a sequence 这个错误通常出现在试图将一个序列(如列表或数组)赋值给数组中的一个位置,而该位置期望的是单个元素(标量)时。 在我们之前的拉曼光谱处理代码中,可能是在将基线校正的结果(一个数组)赋值给DataFrame的一行时出现问题。因为DataFrame的一行应该是一个一维数组,但如果基线校正函数返回的数组形状不符合要求,就会出错。 解决方案: - 确保每次循环中,我们处理的是一个一维数组,并且赋值给DataFrame的一行时,该数组的度必须与列数相同。 - 在之前的代码中,我们使用了`corrected_df.loc[idx] = corrected`,其中`corrected`应该是一个一维数组,且度等于列数。 我们可以通过以下方式避免: corrected_df.loc[idx] = corrected.tolist() # 或者直接使用数组,但确保是一维的 但根据之前的错误,我们可能还需要检查`airpls`函数返回的`corrected`是否为一维数组,且度正确。 2. 解决AttributeError: &#39;dict&#39; object has no attribute &#39;shape&#39; 这个错误是因为我们尝试对一个字典对象使用`.shape`属性。在代码中,我们可能错误地将字典当成了数组或DataFrame。 例如: data = {&#39;a&#39;: 1, &#39;b&#39;: 2} print(data.shape) # 会引发这个错误 解决方案: - 检查代码中所有使用`.shape`的地方,确保该对象是数组(numpy array)或DataFrame(pandas)等具有shape属性的对象。 - 如果确实需要获取字典的大小(元素个数),应使用`len(data)`。 3. 处理字体缺失警告 这个警告通常出现在matplotlib绘图中,当系统中没有指定的字体时发生。我们可以通过以下方式解决: - 使用系统中存在的字体 - 或者下载并安装缺失的字体 - 或者使用默认字体 在代码中,我们可以设置matplotlib使用默认字体来避免警告: import matplotlib.pyplot as plt plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 用来正常显示中文标签 plt.rcParams[&#39;axes.unicode_minus&#39;] = False # 用来正常显示负号 如果出现具体字体缺失的警告,比如找不到&#39;Arial&#39;字体,我们可以: # 方法一:使用其他字体 plt.rcParams[&#39;font.family&#39;] = &#39;sans-serif&#39; plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;, &#39;DejaVu Sans&#39;, &#39;Lucida Grande&#39;, ...] # 指定一系列字体,依次尝试 # 方法二:忽略警告(不推荐) import warnings warnings.filterwarnings(&#39;ignore&#39;, category=UserWarning) 结合之前的拉曼光谱处理代码,我们进行修改: 1. 在基线校正循环中,确保我们正确地处理了数组形状。 2. 检查是否有地方错误地使用了字典的shape属性(在之前的代码中没有发现,但用户提到了,所以需要检查其他部分)。 3. 设置matplotlib的字体。 修改后的代码: 注意:由于用户没有提供完整的代码,我们假设在之前的代码中,我们使用字典来存储一些中间结果,然后错误地使用了.shape。这里我们重点修改基线校正部分并设置字体。 完整修改后的代码示例(只包含主要修改部分): </think> # 解决Python代码中的ValueErrorAttributeError错误及字体缺失警告 针对您遇到的`ValueError: setting an array element with a sequence`、`&#39;dict&#39; object has no attribute &#39;shape&#39;`错误以及字体缺失警告,我将提供系统化的解决方案。 ## 错误原因分析 根据引用内容分析: 1. **ValueError: setting an array element with a sequence**: - 尝试将序列(如列表)赋值给期望单个元素的数组位置 - 数据结构形状不一致(如二维数组赋值给一维数组位置)[^1] 2. **&#39;dict&#39; object has no attribute &#39;shape&#39;**: - Python字典对象没有shape属性(这是NumPy数组的属性) - 错误地将字典当作数组使用 3. **字体缺失警告**: - Matplotlib无法找到指定的系统字体 - 通常出现在中文环境或特殊字体使用场景中 ## 解决方案 ### 1. 解决ValueError: setting an array element with a sequence ```python import numpy as np # 错误案例:尝试将列表赋值给数组元素 arr = np.zeros(3) arr[0] = [1, 2] # 引发ValueError # 正确解决方案: # 方法1:使用正确形状的数组 arr = np.zeros((3, 2)) # 二维数组 arr[0] = [1, 2] # 正确赋值 # 方法2:确保赋值元素是标量 arr = np.zeros(3) arr[0] = 1 # 标量赋值 # 方法3:使用列表推导确保形状一致 data = [[1, 2], [3, 4], [5, 6]] arr = np.array(data) # 从列表创建正确形状的数组 ``` ### 2. 解决&#39;dict&#39; object has no attribute &#39;shape&#39; ```python # 错误案例:尝试获取字典的shape属性 my_dict = {&#39;a&#39;: 1, &#39;b&#39;: 2} shape = my_dict.shape # 引发AttributeError # 正确解决方案: # 方法1:使用列表推导获取字典值形状 values = list(my_dict.values()) shape = len(values) # 获取字典值的数量 # 方法2:转换为NumPy数组后获取形状 import numpy as np arr = np.array(list(my_dict.values())) shape = arr.shape # 正确获取形状(2,) # 方法3:获取字典键值对形状 keys_shape = len(my_dict.keys()) values_shape = len(my_dict.values()) ``` ### 3. 处理字体缺失警告 ```python import matplotlib as mpl import matplotlib.pyplot as plt # 解决方案1:使用系统默认字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;] # 指定中文字体 plt.rcParams[&#39;axes.unicode_minus&#39;] = False # 解决负号显示问题 # 解决方案2:使用内置字体 plt.rcParams[&#39;font.family&#39;] = &#39;sans-serif&#39; # 使用通用无衬线字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;DejaVu Sans&#39;] # 使用内置字体 # 解决方案3:安装并注册缺失字体 from matplotlib.font_manager import FontProperties, findfont, FontManager # 查找字体 font_path = findfont(FontProperties(family=[&#39;Arial&#39;, &#39;SimHei&#39;])) # 注册新字体 fm = FontManager() fm.addfont(&#39;/path/to/your/font.ttf&#39;) # 添加字体文件路径 ``` ### 4. 完整错误处理框架 ```python import numpy as np import pandas as pd import matplotlib.pyplot as plt from matplotlib.font_manager import FontManager def robust_data_processing(data): """处理数据转换中的各种错误""" try: # 处理字典转数组 if isinstance(data, dict): # 安全转换为数组 try: arr = np.array(list(data.values())) print(f"转换后的数组形状: {arr.shape}") return arr except Exception as e: print(f"字典转换错误: {str(e)}") return np.array([]) # 处理数组赋值 elif isinstance(data, np.ndarray): # 确保数组形状一致 if data.ndim > 1: return data.flatten() # 展平多维数组 return data # 处理DataFrame elif isinstance(data, pd.DataFrame): # 确保所有元素是标量 return data.applymap(lambda x: x[0] if isinstance(x, (list, tuple)) else x) return data except Exception as e: print(f"数据处理错误: {str(e)}") return data def configure_matplotlib_fonts(): """配置Matplotlib字体解决警告""" try: # 尝试设置中文字体 plt.rcParams[&#39;font.sans-serif&#39;] = [&#39;SimHei&#39;, &#39;DejaVu Sans&#39;, &#39;Arial&#39;, &#39;sans-serif&#39;] plt.rcParams[&#39;axes.unicode_minus&#39;] = False # 检查字体是否可用 fm = FontManager() font_names = [&#39;SimHei&#39;, &#39;DejaVu Sans&#39;, &#39;Arial&#39;] available_fonts = [f for f in font_names if any(f in font.name for font in fm.ttflist)] if not available_fonts: print("警告: 没有找到合适的字体,使用默认设置") plt.rcParams[&#39;font.family&#39;] = &#39;sans-serif&#39; else: plt.rcParams[&#39;font.sans-serif&#39;] = available_fonts print(f"使用字体: {available_fonts[0]}") except Exception as e: print(f"字体配置错误: {str(e)}") # 使用示例 if __name__ == "__main__": # 1. 配置字体 configure_matplotlib_fonts() # 2. 处理数据错误 problem_dict = {&#39;a&#39;: [1, 2], &#39;b&#39;: [3, 4]} processed_data = robust_data_processing(problem_dict) # 3. 安全赋值 arr = np.zeros(4) try: # 安全赋值 for i in range(len(arr)): if isinstance(processed_data, np.ndarray) and i < len(processed_data): arr[i] = processed_data[i] if processed_data.ndim == 1 else processed_data[i][0] except Exception as e: print(f"赋值错误: {str(e)}") # 4. 绘图验证 plt.figure() plt.plot(arr, label=&#39;处理后的数据&#39;) plt.title(&#39;错误处理演示&#39;) plt.legend() plt.savefig(&#39;error_handling_demo.png&#39;, dpi=120) plt.show() ``` ## 关键解决策略 1. **数据结构验证**: - 使用`isinstance()`检查对象- 数组操作前验证`ndim``shape` $$ \text{安全赋值条件} = \begin{cases} \text{目标维度} = 1 & \text{且} \\ \text{源数据维度} = 1 & \text{且} \\ \text{度匹配} \end{cases} $$ 2. **型转换安全机制**: ```python def safe_convert(value): if isinstance(value, (list, tuple)): return value[0] if len(value) > 0 else 0 return value ``` 3. **字体回退策略**: - 创建字体优先级列表 - 自动选择可用字体 - 提供默认回退方案 4. **错误预防模式**: ```python # 在关键操作前添加保护 if hasattr(data, &#39;shape&#39;): # 安全操作 else: # 替代处理 ``` ## 常见问题排查表 | 错误型 | 检查点 | 工具/方法 | |---------|--------|-----------| | ValueError | 数组形状一致性 | `array.shape`, `np.reshape()` | | AttributeError | 对象型验证 | `type()`, `hasattr()` | | 字体警告 | 字体可用性 | `FontManager().findfont()` | | 数据转换 | 元素型检查 | `isinstance(x, (int, float))` | ## 最佳实践建议 1. **数据操作前验证**: ```python def safe_array_assignment(target, source, index): if not isinstance(target, np.ndarray): raise TypeError("目标必须是NumPy数组") if not isinstance(source, (np.ndarray, list, tuple)): raise TypeError("源数据必须是数组或序列") if isinstance(source, np.ndarray) and source.ndim > 1: source = source.flatten() if index < len(target) and (isinstance(source, np.ndarray) or len(source) > index): target[index] = source[index] if isinstance(source, np.ndarray) else source[index] ``` 2. **跨平台字体解决方案**: ```python def get_platform_font(): import platform system = platform.system() if system == &#39;Windows&#39;: return &#39;SimHei&#39; elif system == &#39;Darwin&#39;: # macOS return &#39;Arial Unicode MS&#39; else: # Linux return &#39;DejaVu Sans&#39; ``` 3. **字典安全访问模式**: ```python # 使用get()避免KeyError value = my_dict.get(&#39;key&#39;, default_value) # 获取"shape"信息 dict_size = len(my_dict) ``` 通过以上方法,您可以系统性地解决这三大问题,并建立更健壮的代码基础。 $$ \text{健壮性} = \alpha \times \text{验证} + \beta \times \text{异常处理} + \gamma \times \text{回退机制} $$
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值