现在vim对我来说可不是一个玩具了,因为现在我发现工作中,有时候是必须写一写些工具,以前用C++写,写一个工具弄一次界面不知道要要编译启动多少次,后来觉得python不用编译也能完成真方便,后来又发现python编辑和调试真坑,再后来发现vim可以和python无缝结合后,在nmap下配置各种快捷键,可以直接调试,生活一下子变的无比美好了,现在vim对我不但只是编辑器,而且是一个可以集合各种工具功能的程序库,所以现在有必要好好复习一下vmscript啦,现在不是算折腾啦,时间的付出也有价值啦.
:h list
:let mylist = [1, two, 3, "four"]
:let emptylist = []
:let nestlist = [[11, 12], [21, 22], [31, 32]]
:let item = mylist[0] " get the first item: 1
:let item = mylist[2] " get the third item: 3
:let item = nestlist[0][1] " get the first list, second item: 12
:let last = mylist[-1] " get the last item: "four"
:echo get(mylist, idx)
:echo get(mylist, idx, "NONE")
:let longlist = mylist + [5, 6]
:let mylist += [7, 8]
:call insert(list, 'a') " prepend item 'a'
:call insert(list, 'a', 3) " insert item 'a' before list[3]
:call add(list, "new") " append String item
:call add(list, [1, 2]) " append a List as one new item
:call extend(list, [1, 2]) " extend the list with two more items
:let i = remove(list, 3) " remove item 3
:unlet list[3] " idem
:let l = remove(list, 3, -1) " remove items 3 to last item
:unlet list[3 : ] " idem
:call filter(list, 'v:val !~ "x"') " remove items with an 'x'
:call sort(list) " sort a list alphabetically
:call reverse(list) " reverse the order of items
:call uniq(sort(list)) " sort and remove duplicates
:for item in mylist
: call Doit(item)
:endfor
:let index = 0
:while index < len(mylist)
: let item = mylist[index]
: :call Doit(item)
: let index = index + 1
:endwhile
:for [lnum, col] in [[1, 3], [2, 8], [3, 0]]
: call Doit(lnum, col)
:endfor
:for [i, j; rest] in listlist
: call Doit(i, j)
: if !empty(rest)
: echo "remainder: " . string(rest)
: endif
:endfor
:let r = call(funcname, list) " call a function with an argument list
:if empty(list) " check if list is empty
:let l = len(list) " number of items in list
:let big = max(list) " maximum value in list
:let small = min(list) " minimum value in list
:let xs = count(list, 'x') " count nr of times 'x' appears in list
:let i = index(list, 'x') " index of first 'x' in list
:let lines = getline(1, 10) " get ten text lines from buffer
:call append('$', lines) " append text lines in buffer
:let list = split("a b c") " create list from items in a string
:let string = join(list, ', ') " create string from list items
:let s = string(list) " String representation of list
:call map(list, '">> " . v:val') " prepend ">> " to each item
:exe 'let sum = ' . join(nrlist, '+'):h dict
:let mydict = {1: 'one', 2: 'two', 3: 'three'}
:let emptydict = {}
:let nestdict = {1: {11: 'a', 12: 'b'}, 2: {21: 'c'}}
:let val = mydict["one"]
:let mydict["four"] = 4
:let val = mydict.one
:let mydict.four = 4
:echo dict.key[idx].key
:for key in keys(mydict)
: echo key . ': ' . mydict[key]
:endfor
:for key in sort(keys(mydict))
:for v in values(mydict)
: echo "value: " . v
:endfor
:for [key, value] in items(mydict)
: echo key . ': ' . value
:endfor
:let onedict = {'a': 1, 'b': 2}
:let adict = onedict
:let adict['a'] = 11
:echo onedict['a']
11
:let dict[4] = "four"
:let dict['one'] = item
:let i = remove(dict, 'aaa')
:unlet dict.aaa
:unlet dict['aaa']
:function Mylen() dict
: return len(self.data)
:endfunction
:let mydict = {'data': [0, 1, 2, 3], 'len': function("Mylen")}
:echo mydict.len()
:let mydict = {'data': [0, 1, 2, 3]}
:function mydict.len()
: return len(self.data)
:endfunction
:echo mydict.len()
:if has_key(dict, 'foo') " TRUE if dict has entry with key "foo"
:if empty(dict) " TRUE if dict is empty
:let l = len(dict) " number of items in dict
:let big = max(dict) " maximum value in dict
:let small = min(dict) " minimum value in dict
:let xs = count(dict, 'x') " count nr of times 'x' appears in dict
:let s = string(dict) " String representation of dict
:call map(dict, '">> " . v:val') " prepend ">> " to each item
list有一个index(o)方法可以找到索引,这个是dist和list同时兼容一种迭代的关键 ...
日常中经常使用字符串和各种数据类型,如何处理以及如何互相之间转换经常使用后会忘记,所以写下一片文章总结整理一下。 1. 字符串 string 1.1 字符串的运算,a=Hello, b=Python 操作符 描述 示例 + 字符串连接 (更多用 join() 函数进行连接) >>> a + b “HelloPython” * 重复输出字符串 >>...
为什么80%的码农都做不了架构师?>>> list 对象 list 对象的定义 list对象内部是使用数组实现,在数组中存储的是指针,指向要保存的对象。 allocated是list中数组的大小,ob_size是当前已经使用的数组大小。 list 对象的缓存 list对象有缓存机制,对象在释放时会保存到空闲缓存池,待下一次申请的时候使用。 缓存池可以缓存...
为什么要谈list/tuple/dict/get 了,作为一个刚开始学习python的菜鸟来说,当我学到函数那一块是各种的各种值数组一累加,我当时就蒙了,还不能完全了解,因此我就它整理了一下希望对更我一样的迷茫的菜鸟有点小小的帮助! 接下来就新介绍这几个词的大致说发与用法吧!(有错误的话还请大神指出来,我会好好改正) list Python内置的一种数据类型是列表:list。list是一种有序的集...
今天面试被问到了这样一道题,不幸的是我答错了 结果是 解释 当list , dict等可变类型作为默认参数时,只会在定义函数的时候执行一次,有点像静态变量,定义一次后面一直使用。在之后使用函数的过程中,如果没有对默认参数进行赋值,那么将会一直使用这个初始的变量。 解决方法 要想避免这种困惑的情况发生,可以按照如下的写法...
1.dict python内置了字典,dict的支持,在其他语言中成为map,使用键-值(key-value)存储,具有极快的查找速度 dict和list比较 1.查找和插入的速度极快,不会随着key的增加而变慢; 2.需要占用大量的内存,内存浪费多; 而list相反: 1.查找和插入的时间随着元素的增加而增加; 2.占用空间小,浪费内存 所以来说,dict使用空间来换取时间的一种方式; 注意: ...
Python之访问dict 我们已经能创建一个dict,用于表示名字和成绩的对应关系: 那么,如何根据名字来查找对应的成绩呢? 可以简单地使用 d[key] 的形式来查找对应的 value,这和 list 很像,不同之处是,list 必须使用索引返回对应的元素,而dict使用key: 注意: 通过 key 访问 dict 的value,只要 key 存在,dict就返回对应的value。如果key...
列表和元组 基本使用 在python 中 列表和元组都是可以放任意数据类型的集合 放的数据类型也可以不同 比如如下代码 二者区别 声明方式 列表是一[]形式的 元组是以 () 形式的 列表是动态的 可以随时进行增删改查 元组是不可变的 就是一旦声明就不会再改变。比如如下例子 当我们向列表或者元组中增加数据时又是怎样的呢 因为列表是动态的 所以直接可以如下操作 直接append 增加 元组是不可变的...
Python 删除一个list的元素: 根据索引删 根据元素删 删除一个dict里的元素: Python复制一个list 复制dict...
uniapp 等图片文件全部上传成功后,再继续执行js代码 起因 前几天在写一个简单表单提交的时候,遇到了一个文件上传的问题。这个表单,需要上传三类图片,而且每一类都是可以多张上传的。 使用文件上传的组件,一般都是选择图片后,他就会自动进行服务器的上传,但是我很难去判断要上传的图片,是不是已经全部上传成功了(后端同学不给写批量上传,又是一个我下决定学学后端的原因)。 我使用了一个定时器,去循环判断...