sample_dict = {'a': 100, 'b': 200, 'c': 300} in_dict(sample_dict, 100) -> True in_dict(sample_dict, 110) -> False
时间: 2024-05-04 12:17:51 浏览: 8
以下是判断字典中是否存在某个值的代码:
```
def in_dict(d, value):
return value in d.values()
```
使用示例:
```
sample_dict = {'a': 100, 'b': 200, 'c': 300}
print(in_dict(sample_dict, 100)) # 输出 True
print(in_dict(sample_dict, 110)) # 输出 False
```
相关问题
class Dn_datasets(Dataset): def __init__(self, data_root, data_dict, transform, load_all=False, to_gray=False, s_factor=1, repeat_crop=1): self.data_root = data_root self.transform = transform self.load_all = load_all self.to_gray = to_gray self.repeat_crop = repeat_crop if self.load_all is False: self.data_dict = data_dict else: self.data_dict = [] for sample_info in data_dict: sample_data = Image.open('/'.join((self.data_root, sample_info['path']))).copy() if sample_data.mode in ['RGBA']: sample_data = sample_data.convert('RGB') width = sample_info['width'] height = sample_info['height'] sample = { 'data': sample_data, 'width': width, 'height': height } self.data_dict.append(sample) def __len__(self): return len(self.data_dict) def __getitem__(self, idx): sample_info = self.data_dict[idx] if self.load_all is False: sample_data = Image.open('/'.join((self.data_root, sample_info['path']))) if sample_data.mode in ['RGBA']: sample_data = sample_data.convert('RGB') else: sample_data = sample_info['data'] if self.to_gray: sample_data = sample_data.convert('L') # crop (w_start, h_start, w_end, h_end) image = sample_data target = sample_data sample = {'image': image, 'target': target} if self.repeat_crop != 1: image_stacks = [] target_stacks = [] for i in range(self.repeat_crop): sample_patch = self.transform(sample) image_stacks.append(sample_patch['image']) target_stacks.append(sample_patch['target']) return torch.stack(image_stacks), torch.stack(target_stacks) else: sample = self.transform(sample) return sample['image'], sample['target']
这段代码是用来创建一个自定义的 PyTorch 数据集类,名为 Dn_datasets。它的构造函数接受四个参数:data_root,data_dict,transform 和 load_all。其中,data_root 是数据集的根目录,data_dict 是一个字典,包含了数据集中每个样本的路径、宽度和高度等信息,transform 是一个用于数据增强的 torchvision.transforms 实例,load_all 是一个布尔值,指示是否将整个数据集加载到内存中。
在 __init__ 函数中,如果 load_all 是 False,那么 self.data_dict 直接赋值为传入的 data_dict;否则,它会遍历 data_dict 中的每个样本,将其加载到内存中,并将其图像数据、宽度和高度信息封装为一个字典,并将其存储到 self.data_dict 中。
__len__ 函数返回数据集的样本数量,__getitem__ 函数接受一个索引 idx,返回该索引对应的样本。如果 load_all 是 False,那么它会从磁盘上读取该样本的图像数据;否则,它会从 self.data_dict 中读取该样本的图像数据。如果 to_gray 是 True,那么它会将图像转换为灰度图。最后,如果 repeat_crop 大于 1,那么它会对该样本进行多次裁剪,并返回多个图像和目标对作为一个元组;否则,它会对该样本进行单次裁剪,并返回一个图像和目标对作为一个元组。
def split_dot(words): dots=[',','.','!','?'] str_data = [] for w in words: ##*****——请在下方补充代码——***** ##*****——请在上方补充代码——***** return(str_data) def get_eng_dicts(datas): w_all_dict = {} for sample in datas: for token in sample.split(" "): ##*****——请在下方补充代码——***** ##*****——请在上方补充代码——***** sort_w_list = sorted(w_all_dict.items(), key=lambda d: d[1], reverse=True) w_keys = [x for x,_ in sort_w_list[:7000-2]] w_keys.insert(0,"<PAD>") w_keys.insert(0,"<UNK>") w_dict = { x:i for i,x in enumerate(w_keys) } i_dict = { i:x for i,x in enumerate(w_keys) } return w_dict,i_dict
在第一个函数的注释中,我们可以看到需要将输入的words字符串列表中的句子分割成单词,并将其中的标点符号去除。我们可以使用Python的内置方法split()来分割字符串,并使用字符串的replace()方法来去除标点符号。
下面是修改后的代码:
```python
def split_dot(words):
dots=[',','.','!','?']
str_data = []
for w in words:
w = w.replace(".", "").replace(",", "").replace("!", "").replace("?", "") # 去除标点符号
str_data += w.split(" ") # 分割字符串
return(str_data)
```
在第二个函数的注释中,我们可以看到需要将datas中的所有单词统计出现次数,并根据出现次数从多到少排序,然后选取出现次数最多的前7000个单词作为词典。我们可以使用Python的collections模块中的Counter类来统计单词出现次数,并使用sorted()函数来排序。
下面是修改后的代码:
```python
from collections import Counter
def get_eng_dicts(datas):
w_all_dict = Counter()
for sample in datas:
words = sample.split(" ")
# 统计单词出现次数
w_all_dict.update(words)
# 按照出现次数从多到少排序
sort_w_list = sorted(w_all_dict.items(), key=lambda d: d[1], reverse=True)
# 选取出现次数最多的前7000个单词作为词典
w_keys = [x for x,_ in sort_w_list[:7000-2]]
w_keys.insert(0,"<PAD>")
w_keys.insert(0,"<UNK>")
w_dict = { x:i for i,x in enumerate(w_keys) }
i_dict = { i:x for i,x in enumerate(w_keys) }
return w_dict,i_dict
```