python奇奇怪怪

python

  • len(X),对ndarray和list等的操作其实是调用这个函数.__len__()
a=[]
a.__len__()
  • 转义字符应为:+'

  • (Pdb) [sample]*10
    [139, 139, 139, 139, 139, 139, 139, 139, 139, 139]

  • np.stack(([sample]*len(curr_new_samples), curr_new_samples), axis=-1)
    stack只需要在指定的维度,添加一维即可。如这里为2.

  • dict赋值for语句:{k:v for v,k in enumerate(seeds)}

  • list赋值for语句:[index_dict[k] for k in seeds]
    out:

not的结合律:很近

not args.no_cuda and torch.cuda.is_available()
not只作用于第一项。

自己写IOstream类

class IOStream():
    def __init__(self, path):
        self.f = open(path, 'a')

    def cprint(self, text):
        print(text)
        self.f.write(text+'\n')
        self.f.flush()

    def close(self):
        self.f.close()

在文件夹utils下只要创建__init__.py文件,即可导入使用。使用:

from utils import io_utils # 导入都导入的时python的模块,即.py文件
io = io_utils.IOStream('checkpoints/' + args.exp_name + '/run.log')
io.cprint(str(args))

tuple in的误区

>>> t2
(1, 2)
>>> t1
(1, 2, 3)
>>> t2 in t1
False

必须是元素在t1里面才是True,若t3=((1,2),(1)),则为True。

>>> t3=((1,2),(1))
>>> t2 in t3
True
posted @ 2021-03-15 17:29  zae  阅读(8)  评论(0)    收藏  举报