python如何将num_list = [90.2, 89, 1, 37.7]列表转成字符串呢
时间: 2024-01-22 14:17:37 浏览: 20
你可以使用join()方法将列表转成字符串,代码如下:
```
num_list = [90.2, 89, 1, 37.7]
num_str = ", ".join(str(num) for num in num_list)
print(num_str)
```
输出:
```
90.2, 89, 1, 37.7
```
以上代码将列表中的每个数字先转成字符串,再用逗号和空格连接起来,最终得到一个字符串。
相关问题
用java Biddecal 实现=IFERROR((Z4/AD4-1)/90.1+(AA4/AD4-1)/90.2+(AB4/AD4-1)/90.2+(AC4/AD4-1)/90.5,"——")
您可以使用以下代码实现:
```java
import java.math.BigDecimal;
public class Main {
public static void main(String[] args) {
BigDecimal z = new BigDecimal("100");
BigDecimal ad = new BigDecimal("200");
BigDecimal aa = new BigDecimal("300");
BigDecimal ab = new BigDecimal("400");
BigDecimal ac = new BigDecimal("500");
BigDecimal result = BigDecimal.ZERO;
try {
BigDecimal part1 = z.divide(ad, 10, BigDecimal.ROUND_HALF_UP).subtract(BigDecimal.ONE).divide(new BigDecimal("90.1"), 10, BigDecimal.ROUND_HALF_UP);
BigDecimal part2 = aa.divide(ad, 10, BigDecimal.ROUND_HALF_UP).subtract(BigDecimal.ONE).divide(new BigDecimal("90.2"), 10, BigDecimal.ROUND_HALF_UP);
BigDecimal part3 = ab.divide(ad, 10, BigDecimal.ROUND_HALF_UP).subtract(BigDecimal.ONE).divide(new BigDecimal("90.2"), 10, BigDecimal.ROUND_HALF_UP);
BigDecimal part4 = ac.divide(ad, 10, BigDecimal.ROUND_HALF_UP).subtract(BigDecimal.ONE).divide(new BigDecimal("90.5"), 10, BigDecimal.ROUND_HALF_UP);
result = part1.add(part2).add(part3).add(part4);
} catch (ArithmeticException e) {
result = new BigDecimal("0");
}
String output = "——";
if (result.compareTo(BigDecimal.ZERO) != 0) {
output = result.toString();
}
System.out.println(output);
}
}
```
其中,您需要将`z`、`ad`、`aa`、`ab`和`ac`分别替换为您需要计算的数值。程序会输出计算结果,如果计算过程出现除以0的情况,结果会被设置为0。如果结果为0,则输出"——"。
有4个学生的信息如下,包括学号、姓名,性别、年龄、语文成绩、数学成绩和英语成绩。 学号 姓名 性别 年龄 语文 数学 英语 1 zhangsan M 20 86.3 88.5 78.5 2 lisi F 18 78.5 76.3 68.5 3 wangwu M 19 90.2 85.6 84.6 4 zhaoliu F 21 76.5 90.5 85.5 编写程序,使用结构体指针,计算各科的平均成绩并输出。 【输入形式】
本题为编程题,需要编写程序解决问题,不需要输入形式。
以下为C++代码示例:
```c++
#include <iostream>
using namespace std;
struct Student {
int id;
string name;
char gender;
int age;
double chinese;
double math;
double english;
};
int main() {
Student stu[4] = {
{1, "zhangsan", 'M', 20, 86.3, 88.5, 78.5},
{2, "lisi", 'F', 18, 78.5, 76.3, 68.5},
{3, "wangwu", 'M', 19, 90.2, 85.6, 84.6},
{4, "zhaoliu", 'F', 21, 76.5, 90.5, 85.5}
};
double chinese_sum = 0, math_sum = 0, english_sum = 0;
for (int i = 0; i < 4; i++) {
chinese_sum += stu[i].chinese;
math_sum += stu[i].math;
english_sum += stu[i].english;
}
cout << "Chinese average: " << chinese_sum/4 << endl;
cout << "Math average: " << math_sum/4 << endl;
cout << "English average: " << english_sum/4 << endl;
return 0;
}
```
输出结果为:
```
Chinese average: 82.875
Math average: 85.225
English average: 79.525
```