'num':'20180101', 'name':'Liming', 'sex':'male' 转换成字典
时间: 2024-01-28 07:02:56 浏览: 167
以下是将给定的字符串转换为字典的示例代码:
```python
str_data = "'num':'20180101', 'name':'Liming', 'sex':'male'"
dict_data = {}
# 去除字符串中的单引号和空格
str_data = str_data.replace("'", "").replace(" ", "")
# 按逗号分割字符串
items = str_data.split(",")
# 遍历每个键值对,将其分割为键和值,并添加到字典中
for item in items:
key, value = item.split(":")
dict_data[key] = value
print(dict_data) # 输出:{'num': '20180101', 'name': 'Liming', 'sex': 'male'}
```
相关问题
将'[{name:"liming",age:18},{name:"liming11",age:19}]'转换成[{name:"liming",age:18},{name:"liming11",age:19}]
这是一个字符串,可以使用 JSON.parse() 方法将其转换为 JavaScript 对象数组:
```
let str = '[{name:"liming",age:18},{name:"liming11",age:19}]';
let arr = JSON.parse(str);
console.log(arr); // [{name:"liming",age:18},{name:"liming11",age:19}]
```
用C++ (多重继承) 声明一个教师类(Teacher): 保护数据成员:string name; //姓名 int age; //年龄 string title; //职称 公有成员西数:Teacher(string n,int a, string t):/构造函数 void display();1/输出教师的有关数据(姓名、年龄、职称) 声明一个学生类(Student): 保护数据成员:string name; /姓名 char sex; //性别 float score; //成绩 公有成员西数:Student(string n, int s, float c):/构造函数 void display();/输出学生的有关数据(姓名、性别、成绩) 声明一个在职研究生(Graduate),公有继承教师类(Teacher)和学生类(Student 私有数据成员:foat wage; 1/工资 公有成员西数:Graduate (string n, char s, int a, string t, nloat c, nloat w);/构造函数void show(;/输出研究生的有关数据(姓名、性别、年龄、职称、成绩、工资) 编写一个程序测试该类,测试数据及要求如下: 主西数中定义在职研究生类对象:Graduate g(“LiMing',’r,24,“助教”,89.5,3000); 本程序的执行结果如下: name: LiMing sex: f age: 24 title:助教 score: 89.5 wage: 3000
```cpp
#include <iostream>
#include <string>
using namespace std;
class Teacher {
protected:
string name;
int age;
string title;
public:
Teacher(string n, int a, string t) : name(n), age(a), title(t){}
void display() {
cout << "name: " << name << endl;
cout << "age: " << age << endl;
cout << "title: " << title << endl;
}
};
class Student {
protected:
string name;
char sex;
float score;
public:
Student(string n, char s, float c) : name(n), sex(s), score(c){}
void display() {
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
cout << "score: " << score << endl;
}
};
class Graduate : public Teacher, public Student {
private:
float wage;
public:
Graduate(string n, char s, int a, string t, float c, float w) : Teacher(n, a, t), Student(n, s, c), wage(w){}
void show() {
cout << "name: " << name << endl;
cout << "sex: " << sex << endl;
cout << "age: " << age << endl;
cout << "title: " << title << endl;
cout << "score: " << score << endl;
cout << "wage: " << wage << endl;
}
};
int main() {
Graduate g("LiMing", 'f', 24, "助教", 89.5, 3000);
g.show();
return 0;
}
```
输出结果:
```
name: LiMing
sex: f
age: 24
title: 助教
score: 89.5
wage: 3000
```
阅读全文