list
list()
Generate / convert a list.
Character string → list
and tuple → list
are possible.
Generate
hoge = []
hoge = list() #Same as above
conversion
list('cat')
# ['c','a','t']
Offset
slice
hoge = ['a','b','c','d']
hoge[::2]
# ['a','c']
hoge[::-2]
# ['d','b'] #From the opposite
hoge[::-1]
# ['d','c','b','a'] #In reverse order
append() extend()
Add and combine
hoge = ['a','b','c','d']
fuga = ['e']
hoge.append('e')
hoge.extend(fuga)
hoge += fuga
# ['a','b','c','d','e'] #All the same result
insert()
piyo = ['a','b','d','e']
piyo.insert(2, 'c')
# ['a','b','c','d','e']
remove() pop()
hoge = ['a','b','c','d','e']
hoge.remove('d') #Delete by specifying the element itself
hoge.pop(3) #Delete by specifying an offset(To be exact, take out)
# hoge = ['a','b','c','e'] #Same result
hoge = ['a','b','c','d','e']
if 'a' in hoge:
print("exist")
else
print("not exist")
# exist
split() join()
fuga = 'a,b,c,d'
fuga.split(',')
# ['a','b','c','d']
hoge = ['a','b','c','d']
','.join(hoge) #hoge.join(',')Note that it is not!
# 'a,b,c,d'
copy()
Python is basically passed by reference.
hoge = ['a','b','c','d']
fuga = hoge
#If you change hoge after this, fuga will also reflect the change.=fuga refers to hoge.
fuga = hoge.copy()
fuga = list(hoge)
fuga = hoge[:]
#Any of these generate a copy rather than a reference, so fuga keeps the original list even if you change the hofge.
hoge = ['a','b','c','d']
for i, val in enumerate(hoge):
print(i, val)
The difference from the list is that it is immutable, that is, the changes do not work.
Create
hoge = 4, 10, 5
hoge = (4, 10, 5)
hoge = tuple(4, 10, 5) #All the same.
The contents of tuples can be assigned to each variable without using temporary variables.
Unpack
hoge = tuple(4, 10, 5)
a, b, c = hoge
dict()
Generate
hoge = {}
hoge = dict()
conversion
fuga = [('a', 'b'), ('c', 'd')] #Lists of lists can also be converted
hoge = dict(fuga)
# {
# 'a': 'b',
# 'c': 'd'
# }
piyo = list(hoge.items()) #Converted with a list of tuples.(In addition, list()If you don't bite the dict_Returns in the form of items.)
# [('a', 'b'), ('c', 'd')]
#The reason for applying as follows is because it uses the unpacked nature of tuples.
for key, item in hoge.items():
pass
Add and combine
hoge = {
'a': 'b',
'c': 'd'
}
hoge['e'] = 'f' #You can specify it without permission
fuga = {
'g': 'h'
}
hoge.update(fuga) #Join
Same as the list.
hoge = {
'a': 'b',
'c': 'd'
}
if 'a' in hoge:
print("exist")
else
print("not exist")
# exist
keys() values()
hoge = {
'a': 'b',
'c': 'd'
}
hoge.keys()
# dict_keys(['a', 'c'])→ Returns an iterable key view. list()Need to bite and convert to a list.
list(hoge.keys())
# ['a', 'c']
values () is the same as above.
copy()
Same as the list. When not passing by reference.
When you don't care about the order. The shape is represented as {0, 2, 7, 1, 4}
.
Create
hoge = set() #Empty set
hoge = {0, 2, 7, 1, 4} #A set that also defines the contents
hoge = {} #This is a dictionary
hoge = set( ["hoge", "fuga", "piyo"] ) #In addition to lists, tuples, character strings, and dictionaries are also supported.(Only key is extracted in the dictionary)
# {"piyo", "hoge", "fuga"}
bytes → Something like a tuple of bytes, immutable. bytearray → Something like a list of bytes, mutable.
zip()
Return the tuple. It can be used as follows by unpacking with a for statement.
hoge = ["a", "b", "c"]
fuga = ["A", "B", "C", "D"]
for small, large in zip(hoge, fuga):
print("small:", small, "large:", large)
# small: a large: A
# small: b large: B
# small: c large: C #Finish according to the shorter one,"D"It doesn't come out.
range()
Creating a sequence. The syntax range (start, end, step)
.
Up to the number just before ʻend`.
range(0, 3)
# 0, 1, 2
Write a for loop without the []
in the list.
Syntax 1
[Expression for variable in iterable object]
Example
hoge = [num for num in range(0, 3)]
# [0, 1, 2]
fuga = [num * 2 for num in range(0, 3)]
# [0, 2, 4]
piyo = [(a, b) for a in range(0, 2) for b in range(0, 3)]
#[(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2)]
Syntax 2
[Expression for variable in iterable object if condition]
Example
hoge = [num for num in range(0, 3) if num % 2 == 0]
# [0, 2]
syntax
{formula:formulafor 変数 in イテラブルオブジェクト }
Example
word = "letter" #It's ok to keep a list or set
hoge = { let : word.count(let) fot let in word }
# { "l": 1, "e": 2, "t": 2, "r": 1}
the same. abridgement
→ There is no inclusion notation in tuples! Writing in the same format as above returns a generator object.
Recommended Posts