1,判斷乙個字串中的每乙個字母是否都在另乙個字串中,可以利用集合的特性來解,集合的元素如果存在,再次更新(update) 是新增不進集合的,那麼集合的長度還是跟原來一樣,如果新增進去,集合長度就會增加
>>> a = 'ghost
'>>> b = '
hello, can you help me install ghost windows xp system
'>>> b_set =set( b )
>>>b_set.update( list( a ) )
>>> print len( b_set ) ==len( set( b ) )
true
>>> a = '
abcostg
'>>>b_set.update( list( a ) )
>>> print len( b_set ) ==len( set( b ) )
false
>>>
2,如果是多個字元呢?
#!/usr/bin/python
#coding:utf-8
#str_list = [ 'abc', 'ghost', 'hello' ]
str_list = [ '
abc', '
ghost
', '
hellox']
target_str = "
abcdefghijklopqrst
"target_str_set =set( target_str )
for val in
str_list:
target_str_set.update( val )
print len( target_str_set ) == len( set( target_str ) )
3,統計出現次數最多的字元
ghostwu@ghostwu:~/python/tmp$ python str3.py[('f', 7), ('
s', 5), ('
a', 4), ('
j', 4), ('
k', 3), ('
h', 2), ('
3', 2), ('
1', 2), ('
2', 2), ('
d', 1), ('
l', 1), ('
4', 1), ('
;', 1)]
ghostwu@ghostwu:~/python/tmp$ cat str3.py
#!/usr/bin/python
#coding:utf-8
str = '
askfjkjasf1234fasdfasfsh;lkjfhjf123
'l = ( [ ( key, str.count( key ) ) for key in
set( str ) ] )
l.sort( key = lambda item : item[1], reverse =true )
lghostwu@ghostwu:~/python/tmp$
這裡有個lambda表示式, key指定按哪個鍵排序, item是形參,代表當前的元組,item[1],那就是取元組中第2項,這裡就是字串的次數,reverse = true,從高到低排序 .
4,統計this模組中, be, is, than,三個單詞的出現次數
ghostwu@ghostwu:~/python/tmp$ !ppython statics.py [('
be', 3), ('
is', 10), ('
than
', 8)]
ghostwu@ghostwu:~/python/tmp$ cat statics.py
#!/usr/bin/python
#coding:utf-8
import
osthis_str = os.popen( "
python -m this
").read()
this_str = this_str.replace( '
\n', ''
)l = this_str.split( '')
print [ ( x, l.count( x ) ) for x in ['
be', '
is', '
than
'] ]
ghostwu@ghostwu:~/python/tmp$
os.popen( "python -m this" ).read 讀出命令列python -m this 模組的執行結果到乙個字串中
5,用位移運算子,換算b, kb, mb之間的轉換關係
ghostwu@ghostwu:~/software$ ls -l sogoupinyin_2.2.0.0102_amd64.deb-rw-rw-r-- 1 ghostwu ghostwu 22852956 2月 2 14:36 sogoupinyin_2.2.0.0102_amd64.deb
ghostwu@ghostwu:~/software$ ls -lh sogoupinyin_2.2.0.0102_amd64.deb
-rw-rw-r-- 1 ghostwu ghostwu 22m 2月 2 14:36 sogoupinyin_2.2.0.0102_amd64.deb
ghostwu@ghostwu:~/software$ python
python 2.7.12 (default, dec 4 2017, 14:50:18)
[gcc 5.4.0 20160609] on linux2
type
"help
", "
", "
credits"or
"license
"for
more information.
>>> size = 22852956
"%s kb
" % ( size >> 10)
22317kb
"%s mb
" % ( size >> 20)
21mb
>>>
6,把列表中的值,連線成字串
>>> a = [10, 20, 30, 1, 2, 3]>>> s =str( a )
>>>s
'[10, 20, 30, 1, 2, 3]
'>>>type( s )
'str
'>
>>> s[1:-1]
'10, 20, 30, 1, 2, 3
'>>> s.replace( '
, ', '', s[1:-1] )
traceback (most recent call last):
file
"", line 1, in
typeerror: an integer
isrequired
>>> s[1:-1].replace( '
, ', '')'
102030123
'>>>
Python基礎綜合練習
畫一面五星紅旗,將 與執行截圖發布部落格交作業 import turtle t turtle.pen t.hideturtle 移動筆 def mygoto x,y t.up t.goto x,y t.down 畫五角星 def dramfive x t.begin fill for i in ra...
Python基礎綜合練習
hey 下一秒向你靠近 夢已準備就緒 show you 我的心絕不放棄 i m the one you want 不需要遲疑 綻放所有光芒shining 只想為你證明 你整個世界被我佔據 i m the one you want絕無可代替 幸運的視角 都為我聚焦 你的每個決定是我渴望的驕傲 讓 猶豫...
DFS技巧有關的訓練題
這個是板子 include include include include include include include include include include include include include using namespace std define rep i,aa,bb f...