A list of major destructive methods (overwriting the original data).
**
**
**
**
insert x y
└ Insert y at index number x.
└ x is an integer
└ If you specify an index number above (or below) the original element, it will be added to the end (beginning).
python
a = [1,2,3]
#Add "10" to 0th
a.insert(0,10)
print(a)
#output
[10, 1, 2, 3]
python
a = [1,2,3]
#Added "20" to the 10th
a.insert(10,20)
print(a)
#output
[1, 2, 3, 20]
Although index number 10 is specified, it is added to the end because the maximum of the target list is less than 10.
** ▼ When a non-existent index number is specified (minus) **
python
a = [1,2,3]
#-Added "30" to the 10th
a.insert(-10,30)
print(a)
#output
[30, 1, 2, 3]
ʻAppend (value to add)`
python
a=[1,2,3]
a.append('a')
print(a)
##output
[1, 2, 3, 'a']
String
a=[1,2,3]
a.append("abcde")
print(a)
##output
[1, 2, 3, 'abcde']
list
a=[1,2,3]
b=[4,5,6]
a.append(b)
print(a)
##output
[1, 2, 3, [4, 5, 6]]
list
a=[1,2,3]
a.append(a)
print(a)
#output
[1, 2, 3, [...]]
list
a=[[1,2,3]]
b=[4,5,6]
a.append(b)
print(a)
#output
[[1, 2, 3], [4, 5, 6]]
It becomes the same dimension.
ʻExtend (element to add)` └ Add one character at a time └ Strings are decomposed
1D list
a=[1,2,3]
b=[4,5,6]
a.extend(b)
print(a)
#output
[1, 2, 3, 4, 5, 6]
String
a=[1,2,3]
a.extend("abcde")
print(a)
#output
[1, 2, 3, 'a', 'b', 'c', 'd', 'e']
python
a=[1,2,3]
a.extend(a)
print(a)
#output
[1, 2, 3, 1, 2, 3]
python
a=[1,2,3]
b=[[4,5,6],[7,8,9]]
a.extend(b)
print(a)
#output
[1, 2, 3, [4, 5, 6], [7, 8, 9]]
Two dimensions
a=[[1,2,3]]
b=[[4,5,6],[7,8,9]]
a.extend(b)
print(a)
#output
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
■ Properties that have both extend and append ・ Add to the same level ・ Add in chunks (do not decompose character by character)
list [x: x] = element to add
└ x: Index number you want to add
└ Specify the same range for the slice range * If they are different, replacement will occur.
Index number 1 is "'aaa','bbb'"Add
a=[1,2,3]
a[1:1]=['aaa','bbb']
print(a)
#output
[1, 'aaa', 'bbb', 2, 3]
minus
a=[1,2,3]
a[-1:-1]=['aaa','bbb']
print(a)
#output
[1, 2, 'aaa', 'bbb', 3]
From the last element, it becomes 0, -1, -2 ,,,.
Out of the plus range
a=[1,2,3]
a[10:10]=['aaa','bbb']
print(a)
#output
[1, 2, 3, 'aaa', 'bbb']
Outside the negative range
a=[1,2,3]
a[-10:-10]=['aaa','bbb']
print(a)
#output
['aaa', 'bbb', 1, 2, 3]
list [x: y] = element to add
└ Replace the range from x to y-1.
└ y is not included
** ▼ Replace nth element **
list[n:n+1]
Replacement of index number 2
a=[1,2,3,4,5,6,7]
a[2:3]=['aaa','bbb']
print(a)
##output
[1, 2, 'aaa', 'bbb', 4, 5, 6, 7]
Replace all at once
a=[1,2,3,4,5,6,7]
a[0:5]=['aaa','bbb']
print(a)
##output
['aaa', 'bbb', 6, 7]
Replace 0-4th range.
python
a=[1,2,3,4,5,6,7]
a[0:10]=['aaa','bbb']
print(a)
#output
['aaa', 'bbb']
** ▼ When out of range is specified 2 **
python
a=[1,2,3,4,5,6,7]
a[10:20]=['aaa','bbb']
print(a)
#output
[1, 2, 3, 4, 5, 6, 7, 'aaa', 'bbb']
python
a=[1,2,3,4,5,6,7]
a[:]=['aaa','bbb']
print(a)
#output
['aaa', 'bbb']
remove (element to remove)
└ If there are multiple, ** only the first element ** deleted
└ Non-existent value is an error
python
a = [1,2,3]
a.remove(1)
print(a)
#output
[2, 3]
** ▼ When there are multiple specified values **
python
a = [1,2,3,1,2,3,1,2,3]
a.remove(2)
print(a)
#output
[1, 3, 1, 2, 3, 1, 2, 3]
Delete only the first element.
pop (index number)
└ Delete the value of the corresponding index number
└ Numbers outside the range are errors
python
a = [1,2,3]
a.pop(2)
print(a)
#output
[1, 2]
Deleted index number 2 (value 3).
▼ Error outside the range
python
a = [1,2,3]
a.pop(-10)
print(a)
#output
IndexError: pop index out of range
python
a = [1,2,3]
a.clear()
print(a)
##output
[]
All the contents disappear. The container (variable) remains.
Although it is not a method for list, elements can be deleted with a del statement.
del list object [index number]
└ Index number specified by slice
python
a=[1,2,3]
del a[0]
print(a)
##output
[2, 3]
Same as pop method.
[Start value: End value] └ The end value is not deleted. (Less than)
Number 3~Remove 4 (5 not included)
b=[1,2,3,4,5,6,7,8,9]
del b[3:5]
print(b)
##output
[1, 2, 3, 6, 7, 8, 9]
Specify the entire range [:]
in the slice.
python
a=[1,2,3]
del a[:]
print(a)
##output
[]
Same as the clear method.
sort()
└ Default (reverse = False)
└ No argument required * Error if entered
Ascending sort
a=[4,2,10,-10]
a.sort()
print(a)
##output
[-10, 2, 4, 10]
Alphabet
a=[]
a.extend("egkacb")
a.sort()
print(a)
##output
['a', 'b', 'c', 'e', 'g', 'k']
sort(reverse=True)
└ Specify "reverse = True" as an option
Descending sort
a=[4,2,10,-10]
a.sort(reverse=True)
print(a)
##output
[10, 4, 2, -10]
Numerical inversion
a=[4,2,10,-10]
a.reverse()
print(a)
##output
[-10, 10, 2, 4]
Alphabet inversion
a=[]
a.extend("egkacb")
a.sort()
print(a)
##output
['b', 'c', 'a', 'k', 'g', 'e']
Because you are modifying the original object. For example -A.append () refers to "a" itself. -There is no such thing as "a.append ()".
not exist
a = [1,2,3]
b = a.append(4)
print(b)
##output
None
Since the non-existent one is assigned to the variable b, the content of b is None.
If you want to add more elements, play with a itself again.
▼ Question When any of insert, print, remove, append, sort, pop, reverse is input, the corresponding process is executed.
--insert a b: Insert the value b at index number a. --remove a: Remove the first value a. --append a: Append the value a at the end. --pop a: Delete the value of index number a --sort: Ascending sort. --reverse: reverse list --print: Output
▼sample input
python
12 #Number of commands to enter
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print
▼sample output
python
[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
▼answer1
python
if __name__ == '__main__':
n = int(input())
lists = [input() for _ in range(n)]
arr=[]
for i in lists:
arr.append(i.split())
res=[]
for j in arr:
if len(j) >= 2:
a=int(j[1])
if len(j) >=3:
b=int(j[2])
#Each process
if j[0]=="insert":
res.insert(a, b)
if j[0]=="print":
print(res)
if j[0]=="remove":
res.remove(a)
if j[0]=="append":
res.append(a)
if j[0]=="sort":
res.sort()
if j[0]=="reverse":
res.reverse()
if j[0]=="pop":
res.pop()
ans=res
python
n = int(input())
l = []
for _ in range(n):
s = input().split()
cmd = s[0]
args = s[1:]
if cmd !="print":
#Create an argument part with a string and combine it with cmd
cmd += "("+ ",".join(args) +")"
#Execute expression (character string in parentheses)
eval("l."+cmd)
else:
print (l)
・ ʻEval ("expression") ` └ Put an expression in the argument * Not a statement
Executes the expression of the string given as an argument. -Example: eval ("list.insert (2,5)")
Recommended Posts