yield data_dict
时间: 2023-06-27 22:07:17 浏览: 58
`yield data_dict` 语句通常用于生成器函数中,它可以将一个字典数据结构返回给调用者,并且暂停函数的执行,等待下一次调用继续执行。在每次调用时,生成器函数会返回一个字典对象,该字典对象包含一个或多个键值对,这些键值对是根据生成器函数的逻辑计算出来的。通过使用 `yield` 语句,生成器函数可以有效地处理大量数据,并且不会因为内存限制而导致程序崩溃。在Python中,生成器函数常用于迭代操作,比如遍历大型文件或数据库查询结果。
相关问题
将 for index, adcode_dict in enumerate(adcode_list, 1): # 获取adcode 用于拼接完整的url adcode = adcode_dict["adcode"] # 拼接完整的url full_url = base_url + adcode # 发送请求获取天气json数据 response = requests.get(full_url) json_data = response.json() # 第一种方式 # msg = json_data.get("data").get("message") # if msg == "Successful.": # # 获取天气信息 # weather_name = json_data.get("data").get("data")[0].get("live").get("weather_name") # # print(weather_name) # # # 将天气信息 加入到adcode_dict中 # adcode_dict["weather_name"] = weather_name # print(c, adcode_dict) # else: # print(msg) # 第二种方式 try: # 获取天气信息 weather_name = json_data.get("data").get("data")[0].get("live").get("weather_name") # 将天气信息加入到adcode_dict中 adcode_dict["weather_name"] = weather_name if weather_name == '晴': sunny += 1 elif weather_name == '多云': cloud += 1 elif weather_name == '阴': being_cloud += 1 elif '雨' in weather_name: rain += 1 else: haze += 1 print(index, adcode_dict) except Exception as e: print(e)变成一个生成器函数
可以将上述代码改写成生成器函数,每次yield返回一个包含天气信息的adcode_dict字典,例如:
```python
import requests
def get_weather_info(adcode_list):
base_url = "http://www.weather.com.cn/data/cityinfo/"
sunny = cloud = being_cloud = rain = haze = 0
for index, adcode_dict in enumerate(adcode_list, 1):
adcode = adcode_dict["adcode"]
full_url = base_url + adcode
response = requests.get(full_url)
json_data = response.json()
try:
weather_name = json_data.get("weatherinfo").get("weather")
adcode_dict["weather_name"] = weather_name
yield adcode_dict
if weather_name == '晴':
sunny += 1
elif weather_name == '多云':
cloud += 1
elif weather_name == '阴':
being_cloud += 1
elif '雨' in weather_name:
rain += 1
else:
haze += 1
except Exception as e:
print(e)
print("晴天数量:", sunny)
print("多云数量:", cloud)
print("阴天数量:", being_cloud)
print("雨天数量:", rain)
print("雾霾天数量:", haze)
```
调用该生成器函数可以使用for循环来遍历生成器对象,例如:
```python
adcode_list = [{"adcode": "101010100", "city": "北京"},
{"adcode": "101020100", "city": "上海"},
{"adcode": "101280101", "city": "广州"},
{"adcode": "101280601", "city": "深圳"}]
for adcode_dict in get_weather_info(adcode_list):
print(adcode_dict)
```
这将输出每个城市对应的天气信息,并统计不同天气类型的数量。
def train_r(buffered_size=1024): def reader(): xs=[] ys=[] for i in range(1, 6): train_dict = unpickle("/home/aistudio/cifar-10-batches-py/data_batch_%d" % (i,)) xs.append(train_dict[b'data']) ys.append(train_dict[b'labels']) Xtr = np.concatenate(xs) Ytr = np.concatenate(ys) for (x, y) in zip(Xtr, Ytr): yield x, int(y) return paddle.reader.xmap_readers(train_mapper, reader, cpu_count(), buffered_size) # 对自定义数据集创建训练集test 的reader def test_r(buffered_size=1024): def reader(): test_dict = unpickle("/home/aistudio/cifar-10-batches-py/test_batch") X = test_dict[b'data'] Y = test_dict[b'labels'] for (x, y) in zip(X, Y): yield x, int(y) return paddle.reader.xmap_readers(test_mapper, reader, cpu_count(), buffered_size),这段程序如何改成我自己的数据集集啊,我数据集的路径在这儿D:\wjd\tpfl
你可以按照以下步骤修改代码中的数据集路径,以适应你自己的数据集:
1. 将数据集放置在你的电脑中,并记下数据集的路径。
2. 修改 train_r 函数中的数据集路径:
```python
train_dict = unpickle("数据集路径")
```
3. 修改 test_r 函数中的数据集路径:
```python
test_dict = unpickle("数据集路径")
```
4. 将修改后的代码保存并运行即可。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)