其他操作:
print(type(None))
print(bool(0))
print(bool(None))
print(bool(""))
print(bool([]))
print(bool({}))
print(bool(()))
mylist
= [ ix
for ix
in range(10, 20) ]
print(mylist
)
mygen
= ( ix
for ix
in range(10, 20) )
print(mygen
)
字符串的操作:
message
= "what do you like?"
response
= 'spam'
print(response
.upper
())
print(message
.capitalize
())
print(message
.title
())
return_value
= print('abc')
print(return_value
)
fox
= "tHe qUICk bROWn fOx. 000 "
print(fox
.upper
())
print(fox
.lower
())
print(fox
.title
())
print(fox
.capitalize
())
print(fox
.swapcase
())
print(fox
.strip
())
print(fox
.rstrip
())
print(fox
.lstrip
())
print(fox
.strip
('0'))
print(fox
.center
(50))
print(fox
.ljust
(30))
print(fox
.rjust
(30))
数字的操作:
x
= 0.125
print(x
.as_integer_ratio
())
列表的排序:
l
= [4, 2, 7, 4, 9, 1]
print(sorted([4, 2, 7, 4, 9, 1]))
l
.sort
()
print(l
)
print(sorted(['pear', 'apple', 'orange', 'cranberry']))
data
=[{'first':'Guido', 'last':'Van Rossum', 'YOB':1956},
{'first':'Grace', 'last':'Hopper', 'YOB':1906},
{'first':'Alan', 'last':'Turing', 'YOB':1912}]
result
= sorted(data
, key
=lambda item
: item
['YOB'])
print(result
)
adict
= {'a': 24, 'g': 52, 'i': 12, 'k': 33}
result
= sorted(adict
.items
( ), key
= lambda x
: x
[1])
print(dict(result
))
alist
= [{'name': 'a', 'age': 20}, {'name': 'b', 'age': 30}, {'name': 'c', 'age': 25}]
alist
.sort
(key
= lambda x
: x
['age'], reverse
= True)
print(alist
)
N
= 100
print([[x
for x
in range(1, 100)][i
:i
+ 3] for i
in range(0, 100, 3)])
列表或元组——》转化为字典:
dict2
= dict([('name','earth'),('port','80')])
dict1
= dict((['name','earth'],['port','80']))
print(dict1
)
print(dict2
)
字典的操作:
primes
= {2, 3, 5, 7}
odds
= {1, 3, 5, 7, 9}
print(primes
| odds
)
print(primes
.union
(odds
))
print(primes
& odds
)
print(primes
.intersection
(odds
))
print(primes
- odds
)
print(primes
.difference
(odds
))
print(primes
^ odds
)
print(primes
.symmetric_difference
(odds
))
from functools
import reduce
from random
import randint
, sample
s1
= {x
: randint
(1, 4) for x
in sample
('abfcdrg', randint
(1, 5))}
s2
= {x
: randint
(1, 4) for x
in sample
('abfcdrg', randint
(1, 5))}
s3
= {x
: randint
(1, 4) for x
in sample
('abfcdrg', randint
(1, 5))}
print(s1
)
print(s2
)
print(s3
)
result
= reduce(lambda x
, y
: x
& y
, map(dict.keys
, [s1
, s2
, s3
]))
print(result
)
dict_keys
= map(dict.keys
, [s1
, s2
, s3
])
print(*dict_keys
)
创建迭代器:
1.迭代器是一个可以记住遍历的位置的对象。
2.迭代器有两个基本的方法:
iter() 和
next()。
3.字符串,列表,字典或元组对象都可用于创建迭代器
4.内置函数和自定义函数也可用于创建迭代器
it
=iter(range(10))
print(next(it
))
it_01
= iter([1,2,3])
print(next(it_01
))
it_02
=iter((1,2,3))
print(next(it_02
))
it_03
=iter({1:"a",2:"b",3:"c"})
print(next(it_03
))
it_04
= iter({"a":1,"b":2,"c":3})
print(next(it_04
))
it_05
=iter(set((1,2,3)))
print(next(it_05
))
it_06
=iter("xyz")
print(next(it_06
))
m
= map(str, range(10))
print(m
)
print(next(m
))
print(list(m
))
def get_letter(i
):
return chr(ord('a')+i
)
m
=map(get_letter
, range(10))
print(m
)
print(next(m
))
print(list(m
))
迭代器的操作:
from itertools
import count
factors
= [2, 3, 5, 7]
G
= (i
for i
in count
() if all(i
% n
> 0 for n
in factors
))
for val
in G
:
print(val
, end
=' ')
if val
> 40: break
生成器的操作:
def substituter(seq
, substitutions
):
for item
in seq
:
if item
in set(substitutions
):
yield substitutions
[item
]
else:
yield item
s
= 'hello world and everyone in the world'
subs
= {'hello': 'goodbye', 'world': 'galaxy'}
for word
in substituter
(s
.split
(), subs
):
print(word
)
def gen():
for n
in range(12):
yield n
** 2
G1
= (n
** 2 for n
in range(12))
G2
= gen
()
print(*G1
)
print(*G2
)
print(list(G1
))
print(list(G2
))
扩展
用sublime编译器,没报错,风扇几秒后慢慢的飞了
from itertools
import count
factors
= [2, 3, 5, 7]
G
= (i
for i
in count
() if all(i
% n
> 0 for n
in factors
))
for val
in G
:
print(val
, end
=' ')
if val
> 40: break
扩展列表操作
import math
from itertools
import chain
, islice
, groupby
, cycle
from functools
import reduce
def get_letter(i
):
return chr(ord("a") + i
)
def sum_of_squares(acc
, i
):
return acc
+ i
** 2
def notby2or3(n
):
return math
.gcd
(n
, 2) == 1 and math
.gcd
(n
, 3) == 1
def forth_back_forth(n
):
yield range(n
)
yield range(n
, 0, -1)
yield range(n
)
def zigzag_iters(period
):
while True:
yield range(period
)
yield range(period
, 0, -1)
def zigzag(period
):
while True:
yield from range(period
)
yield from range(period
, 0, -1)
def divide_by_3(n
):
return n
// 3
print(list((n
for n
in range(1, 20) if math
.gcd
(n
, 2) == 1 and math
.gcd
(n
, 3) == 1)))
print(list(filter(notby2or3
, range(1, 20))))
print(list(chain
(range(5), range(5, 0, -1), range(5))))
print(list(chain
(*forth_back_forth
(3))))
it
= chain
.from_iterable
(zigzag_iters
(2))
print(next(it
))
print(next(it
))
print(next(it
))
print(next(it
))
print(next(it
))
print(list(islice
(zigzag
(5), 0, 20)))
print(list(islice
(zigzag
(5), 1, 20)))
print(list(islice
(zigzag
(5), 0, 20, 2)))
it
= groupby
(range(12), divide_by_3
)
print(next(it
))
print(next(it
))
print(next(it
))
print(next(it
))
for quadrant
, group_it
in groupby
(range(12), divide_by_3
):
"""
输出:
象限中的项目: 0
0 1 2
象限中的项目: 1
3 4 5
象限中的项目: 2
6 7 8
象限中的项目: 3
9 10 11
"""
print('\n象限中的项目:', quadrant
)
for i
in group_it
:
print(f
"\t{i}", end
= " ")
print("\n", list(islice
(cycle
(range(12)), 32)))
转载请注明原文地址: https://mac.8miu.com/read-504094.html