学習ノート:Python面接100講(Python 3.xベース)02-リスト、メタグループ、辞書
5573 ワード
01-リストまたはメタグループで重複する要素を削除
02-集合の集合と交差
03-2つのリストの先頭と末尾が接続されています
04-乱順リスト
05-単星演算子と双星演算子
06-辞書のkeyとvalueをすばやく置き換える
07-2つのリスト(タプル)を1つの辞書にまとめる
08-リストとタプルの違い
09-リストのソート方法
10-リスト要素はオブジェクトです.ソート方法
11-delとpopの違い
12-lambda関数でリストをソートする
13-ディクショナリキー値がサポートするデータ型
14-ジェネレータタイプのオブジェクトにスライスを使用する
15-ループパラメータのリストをジェネレータにする
16-python辞書とjson文字列の交換方法
python ,
( )
a = [1,2,3] #
b = (1,2,3) #
c = {1,2,3} #
:
1. ,
2. ,
( ) ,
:
a = [1,3,3,3,4]
b = list(set(a))
02-集合の集合と交差
,
、 ,
:
1. add() , remove()
:x={1,3,4,5}
x.add(2)
x.remove(1)
2.
:
if x.__contains__(1):
x.remove(1)
else:
pass
1. :x1 | x2 x1.union(x2)
:x1 & x2 x1.intersection(x2)
2.x1.difference(x2) #x1 , x2
x1 ^ x2 # x1 x2
03-2つのリストの先頭と末尾が接続されています
:
1. extend() , a.extend(b)
, extend
2. , extend
,extend ,
04-乱順リスト
:
1.
import random
'''
,
'''
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
def random_list1(a):
for i in range(0, 100):
index1 = random.randint(0, len(a) - 1)
index2 = random.randint(0, len(a) - 1)
a[index1], a[index2] = a[index2], a[index1]
return a
b = random_list1(a)
print(b)
'''
,
'''
a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
def random_list2(a):
a_copy = a.copy()
result = []
for i in range(0, len(a)):
index = random.randint(0, len(a_copy) - 1)
result.append(a_copy[index])
del a_copy[index]
return result
b = random_list2(a)
print(b)
random.shuffle()
def random_list2(a):
random.shuffle(a)
return a
05-単星演算子と双星演算子
(*) (**) ,
:
(*):
1.
2. ( )
3. , ,
:
def fun1(param1, *param2,x):
pass
fun1(1,2,3,4,6,7,x=8)
(**):
:
def fun2(param1,**param2):
pass
dun2(1,a=1,b=2,c=3)
(*) (**)
:
a b
a=[1,2,3]
b=[4,5,6]
c=[*a,*b]
A B
A={'A':1,'B':2}
B={'C':3,'D':4}
C={**A,**B}
06-辞書のkeyとvalueをすばやく置き換える
key value
0 100
:
D={'A':1,'B':2}
E={v:k for k,v in D}
A=[i for i in range(0,101)]
07-2つのリスト(タプル)を1つの辞書にまとめる
a = ["a","b"]
b = [1,2]
:{"a":1,"b":2}
:
zip dict ( zip , dict )
dict(zip(a,b))
:
c = [[1,2],[3,4]]
, append
:
c = [[1, 2], [3, 4]]
a = ["a", "b"]
r = []
for li in c:
d = dict(zip(a, li))
r.append(d)
print(r)
08-リストとタプルの違い
:
1.
a = (1,2,3,4) #
b = [1,2,3,4] #
2. ,
3. ( tuple , ; list , )
copy_a = tuple(a)
copy_b = list(b)
print(copy_a is a) #True
print(copy_b is b) #False
4. , , , ,
print(a.__sizeof__())
print(b.__sizeof__())
09-リストのソート方法
sort sorted ,
:
sort , sorted
sort ,sorted ,
( sort sorted reverse true )
:
a.sort(reverse=true)
sorted(a, reverse=true)
10-リスト要素はオブジェクトです.ソート方法
,
,
:
1. magic
2.operator
3.sort sorted
1. magic __lt__,__gt___, __lt__
class myClass:
def __init__(self, value):
self.value = value
def __lt__(self, other):
return other.value > self.value
# __lt__ , sort , sorted , , magic
# __gt__ , sort , sorted , , magic
2. operator
:
import operator
a.sort(key=operator.attrgetter("value") #
sorted(a,key=operator.attrgetter("value")
1.magic
def __lt__(self, other):
return self.value > other.value
2.operater
import operater
a.sort(key=operater.attrgetter("value"),reverse=True)
11-delとpopの違い
del pop ,
:
del ,
del a[2]
pop , ,
a.pop(2)
a.pop() #
12-lambda関数でリストをソートする
, lambda
:
a = [
{"name": "Mike", "age": 112},
{"name": "Jack", "age": 23}
]
print(sorted(a, key=lambda x: x["age"])) # reverse
13-ディクショナリキー値がサポートするデータ型
:
key
14-ジェネレータタイプのオブジェクトにスライスを使用する
:
, ,
,itertools islice
islice , ,
from itertools import islice
gen = iter(range(10))
print type(gen)
for i in islice(gen,2,6):
print(i)
15-ループパラメータのリストをジェネレータにする
【i for i in range(10)]
:
,(i for i in range(10))
for,
16-python辞書とjson文字列の交換方法
python json
:
json:
import json
json_str=json,dumps(d)
json :
import json
d = json.loads(json_str)