标签:eva 字符串 www enumerate 条件 添加 sudo 转换 执行
1.ubuntu下让python脚本可直接运行:
test.py文件(后缀可省)
#!/usr/bin/python
print(‘wwwww‘)
sudo chmod +x ./test.py (sudo chmod 755 ./2)
执行./test.py即可
可添加为快捷方式:
sudo ln -s /home/zh/桌面/test.py /usr/bin/test
执行test
2.取出0-20中,能够被3整除的数(列表解析)
>>> [x for x in range(0,20) if x % 3 ==0]
[0, 3, 6, 9, 12, 15, 18]
>>>
3.运行一个脚本
>>> a="{1:1,2:2,‘1‘:22}"
>>> eval(a)
{‘1‘: 22, 1: 1, 2: 2}
>>> b=eval(a)
>>> type(b)
<type ‘dict‘>
>>> eval(‘list("qqqqqqqqqqq")‘)
[‘q‘, ‘q‘, ‘q‘, ‘q‘, ‘q‘, ‘q‘, ‘q‘, ‘q‘, ‘q‘, ‘q‘, ‘q‘]
4.字符与数字转换
>>> chr(115)
‘s‘
>>> ord(‘c‘)
99
5.字符串格式化
>>> ‘aaaa %s bbbbb %s ccccc‘ % (1,2)
‘aaaa 1 bbbbb 2 ccccc‘
>>>
>>> t=‘{}+{}={}‘.format(1,2,3)
>>> t
‘1+2=3‘
>>>
6.字典与列表解析
>>> dict([(1,1),(2,2)])
{1: 1, 2: 2}
>>> zip([1,2,3],[1,2,3])
[(1, 1), (2, 2), (3, 3)]
>>> {a:b*2 for (a,b) in [(1, 1), (2, 2), (3, 3)]}
{1: 2, 2: 4, 3: 6}
>>>
7.实现类似python3的打印
>>> from __future__ import print_function
>>> for line in open(‘/home/zh/桌面/笔记‘):print(line,‘‘)
8.赋值语句中进行条件判断
>>> c=‘1111122222333334444455555‘
>>> d=c[:5]+‘#‘+c[5:] if len(c)<5 else (c[:5]+‘#‘+c[5:10]+‘#‘+c[10:] if len(c)==15 else (c[:5]+‘#‘+c[5:10]+‘#‘+c[10:15]+‘#‘+c[15:] if len(c)==20 else (c[:5]+‘#‘+c[5:10]+‘#‘+c[10:15]+‘#‘+c[15:20]+‘#‘+c[20:] if len(c)==25 else 3)))
>>> d
‘11111#22222#33333#44444#55555‘
>>>
9.zip与map,enumerate
>>> a
[1, 2, 3]
>>> b
[5, 6, 7]
>>> zip(a,b)
[(1, 5), (2, 6), (3, 7)]
>>> type(zip(a,b))
<type ‘list‘>
>>> map(chr,a)
[‘\x01‘, ‘\x02‘, ‘\x03‘]
>>> p=[‘1‘,‘2‘,‘3‘]
>>> enumerate(p)
<enumerate object at 0x7f93e36688c0>
>>> type(enumerate(p))
<type ‘enumerate‘>
>>> for ii in enumerate(p):print(ii)
...
(0, ‘1‘)
(1, ‘2‘)
(2, ‘3‘)
>>> n=enumerate(p)
>>> next(n)
(0, ‘1‘)
>>>
5.获取帮助
dir(sys)
help(sys)
标签:eva 字符串 www enumerate 条件 添加 sudo 转换 执行
原文地址:https://www.cnblogs.com/Micang/p/9501043.html