From http://www.amazon.co.jp/dp/4873117380
-- [:]` `` extracts the entire sequence from beginning to end --
[start:] `` extracts the sequence from the start offset to the end --``` [: end]
extracts the sequence from the beginning to the end-1 offset --``` [start: end] ``
extracts the sequence from start offset to end-1 offset
--``` [start: end: step] `` `extracts the sequence from start offset to end-1 offset for each step character
The offset is 0, 1, ... from zero to the right and -1, -2, ... from the end to the left. If start is not specified, the slice uses 0 (start).
In [9]: sample_list = ['a','b','c','d','e']
In [10]: sample_list[0]
Out[10]: 'a'
In [11]: sample_list[0:]
Out[11]: ['a', 'b', 'c', 'd', 'e']
In [12]: sample_list[0:-1]
Out[12]: ['a', 'b', 'c', 'd']
In [13]: sample_list[0:4]
Out[13]: ['a', 'b', 'c', 'd']
In [19]: sample_list[-3:]
Out[19]: ['c', 'd', 'e']
In [22]: sample_list[-3:-1]
Out[22]: ['c', 'd']
#When flipping the list
In [14]: sample_list[::-1]
Out[14]: ['e', 'd', 'c', 'b', 'a']
append()
Use the ʻappend ()
`method when you want to add an element to the end of the list
In [23]: sample_list.append('f')
In [24]: sample_list
Out[24]: ['a', 'b', 'c', 'd', 'e', 'f']
You can use extend () to combine the two lists into one. This is a destructive operation.
In [27]: sample_list.extend(number_list)
In [28]: sample_list
Out[28]: ['a', 'b', 'c', 'd', 'e', 'f', 1, 2, 3]
# sample_Initialize list
In [33]: sample_list = ['a','b','c','d','e']
In [34]: sample_list += number_list
In [35]: sample_list
Out[35]: ['a', 'b', 'c', 'd', 'e', 1, 2, 3]
By the way, append () is treated as one element, so
In [36]: sample_list = ['a','b','c','d','e']
In [37]: sample_list.append(number_list)
In [38]: sample_list
Out[38]: ['a', 'b', 'c', 'd', 'e', [1, 2, 3]]
The append () function can only append an element at the end of the list. Use insert () when you want to specify the offset of the list and add an element before it. If offset 0 is specified, it will be inserted at the beginning. If you specify an offset beyond the end, it will be inserted at the end of the list, just like append ().
In [42]: sample_list = ['a','b','c','d','e']
In [43]: sample_list.insert(1,1)
In [44]: sample_list
Out[44]: ['a', 1, 'b', 'c', 'd', 'e']
In [45]: sample_list.insert(10,10)
In [46]: sample_list
Out[46]: ['a', 1, 'b', 'c', 'd', 'e', 10]
Note that del is a statement, not a method.
In [46]: sample_list
Out[46]: ['a', 1, 'b', 'c', 'd', 'e', 10]
In [48]: del sample_list[-1]
In [49]: sample_list
Out[49]: ['a', 1, 'b', 'c', 'd', 'e']
(Del, len, and non-unified implementations are pretty confusing ..)
If you don't know the offset, or if you don't care where it is, you can use remove () to specify a value to remove the element.
In [49]: sample_list
Out[49]: ['a', 1, 'b', 'c', 'd', 'e']
In [50]: sample_list.remove(1)
In [51]: sample_list
Out[51]: ['a', 'b', 'c', 'd', 'e']
You can use pop () to pop an element out of the list and remove it from the list at the same time. Calling pop () with an offset returns the element for that offset. If no argument is given, -1 is used as the offset.
In [51]: sample_list
Out[51]: ['a', 'b', 'c', 'd', 'e']
In [52]: sample_list.pop()
Out[52]: 'e'
In [53]: sample_list
Out[53]: ['a', 'b', 'c', 'd']
In [54]: sample_list.pop(1)
Out[54]: 'b'
In [55]: sample_list
Out[55]: ['a', 'c', 'd']
In [56]: sample_list = ['a','b','c','d','e']
In [57]: sample_list.append('a')
In [58]: sample_list
Out[58]: ['a', 'b', 'c', 'd', 'e', 'a']
In [60]: sample_list.index('b')
Out[60]: 1
#If there are multiple, the one with the smaller offset
In [61]: sample_list.index('a')
Out[61]: 0
Use in to test if the list has a value.
In [64]: sample_list
Out[64]: ['a', 'b', 'c', 'd', 'e']
In [65]: 'a' in sample_list
Out[65]: True
In [66]: 'g' in sample_list
Out[66]: False
Use count () to count how many specific values are in the list.
In [64]: sample_list
Out[64]: ['a', 'b', 'c', 'd', 'e']
In [67]: sample_list.count('a')
Out[67]: 1
In [68]: sample_list.count('g')
Out[68]: 0
In [69]: sample_list.append('a')
In [70]: sample_list
Out[70]: ['a', 'b', 'c', 'd', 'e', 'a']
In [71]: sample_list.count('a')
Out[71]: 2
There are two sorts --Method sort () is a destructive operation --The general-purpose function sorted () is a non-destructive operation. --Returns a copy of the sorted list
In [76]: names = [ 'Ryoma', 'Toshimichi', 'Tomoatsu']
In [77]: sorted_named = sorted(names)
In [78]: sorted_named
Out[78]: ['Ryoma', 'Tomoatsu', 'Toshimichi']
In [79]: names.sort()
In [80]: names
Out[80]: ['Ryoma', 'Tomoatsu', 'Toshimichi']
Integers and floats can be used together because Python automatically converts them in the expression.
In [81]: numbers = [2, 1, 4.0, 3]
In [82]: numbers.sort()
In [83]: numbers
Out[83]: [1, 2, 3, 4.0]
By default, it is in ascending order,```reverse=True``It can also be done in descending order.
In [84]: numbers = [2, 1, 4.0, 3]
In [85]: numbers.sort(reverse=True)
In [86]: numbers
Out[86]: [4.0, 3, 2, 1]
=
Then, note that the reference destination is the same.
In [87]: a = [1, 2, 3]
In [88]: b = a
In [89]: b[0] = 'why'
In [90]: b
Out[90]: ['why', 2, 3]
In [91]: a
Out[91]: ['why', 2, 3]
Use one of the following to copy -List copy()function
In [92]: a = [1, 2, 3]
In [93]: b = a.copy()
In [94]: c = list(a)
In [95]: d = a[:]
In [96]: a
Out[96]: [1, 2, 3]
In [97]: b
Out[97]: [1, 2, 3]
In [98]: c
Out[98]: [1, 2, 3]
In [99]: d
Out[99]: [1, 2, 3]
In [100]: a[0] = 'why'
In [101]: a
Out[101]: ['why', 2, 3]
In [102]: b
Out[102]: [1, 2, 3]
In [103]: c
Out[103]: [1, 2, 3]
In [104]: d
Out[104]: [1, 2, 3]
list()The conversion function generates a list when used alone, but when taking a list or tuple as an argument, it expands the outermost part of the contents and lists it.
In [107]: a = [1, 2, 3]
In [108]: b = (1, 2, 3)
In [109]: c = list(a)
In [110]: c
Out[110]: [1, 2, 3]
In [111]: d = list(b)
In [112]: d
Out[112]: [1, 2, 3]
In [113]: a.append(b)
In [114]: a
Out[114]: [1, 2, 3, (1, 2, 3)]
In [115]: c = list(a)
In [116]: c
Out[116]: [1, 2, 3, (1, 2, 3)]
Recommended Posts