情報学部 | 菅沼ホーム | 目次 | 索引 |
>>> from bisect import * >>> a = [1, 3, 5] >>> bisect_right(a, 3) 2 >>> bisect_left(a, 3) 1
>>> from bisect import * >>> a = [] >>> insort_right(a, 5) >>> a [5] >>> insort_right(a, 1) >>> a [1, 5] >>> insort_right(a, 10) >>> a [1, 5, 10] >>> insort_right(a, 7) >>> a [1, 5, 7, 10]
01 # -*- coding: UTF-8 -*- 02 x = [1, 2, 3] 03 y = x 04 y[0] = 10 05 print(x, y) # [10, 2, 3] [10, 2, 3]
01 # -*- coding: UTF-8 -*- 02 import copy 03 x = [1, 2, 3] 04 y = copy.copy(x) 05 y[0] = 10 06 print(x, y) # [1, 2, 3] [10, 2, 3]
01 # -*- coding: UTF-8 -*- 02 import copy 03 x = [[1, 2, 3], [4, 5, 6]] 04 y = copy.copy(x) 05 y[0][0] = 10 06 print(x,y) # [[10, 2, 3], [4, 5, 6]] [[10, 2, 3], [4, 5, 6]]
01 # -*- coding: UTF-8 -*- 02 import copy 03 04 class Example : 05 def __init__(self, x) : 06 self.x = x 07 08 x = [Example(1), Example(2), Example(3)] 09 y = copy.copy(x) 10 y[1] = Example(4) 11 y[2].x = 5 12 print(x[0].x, x[1].x, x[2].x) # 1 2 5 13 print(y[0].x, y[1].x, y[2].x) # 1 4 5
01 # -*- coding: UTF-8 -*- 02 import copy 03 04 class Example : 05 def __init__(self, x) : 06 self.x = x 07 08 x = [[1, 2, 3], [4, 5, 6]] 09 y = copy.deepcopy(x) 10 y[0][0] = 10 11 print(x, y) # [[1, 2, 3], [4, 5, 6]] [[10, 2, 3], [4, 5, 6]] 12 13 x = [Example(1), Example(2), Example(3)] 14 y = copy.deepcopy(x) 15 y[1] = Example(4) 16 y[2].x = 5 17 print(x[0].x, x[1].x, x[2].x) # 1 2 3 18 print(y[0].x, y[1].x, y[2].x) # 1 4 5
>>> from datetime import * >>> x = timedelta(days = 10) >>> x.days 10 >>> date.today() + x # 今日から 10 日後は何日か? datetime.date(2016, 9, 1)
>>> from datetime import * >>> date(2020, 9, 1) - date(2016, 9, 1) datetime.timedelta(1461) # 2020/9/1 は 2016/9/1 から 1461 日目
>>> from datetime import * >>> x = date.today() >>> x datetime.date(2016, 8, 22) # 今日は,2016/8/22 >>> x.toordinal() 736198 # 1/1/1 から 736198 日目 >>> date.fromordinal(736198) datetime.date(2016, 8, 22) # 1/1/1 から 736198 日目は,2016/8/22
>>> from datetime import * >>> date.today() datetime.date(2016, 8, 22) # 今日は,2016/8/22
>>> from datetime import * >>> x = date.today() >>> x.isocalendar() (2016, 34, 1) >>> x.isoformat() '2016-08-22'
>>> from datetime import * >>> x = date.today() >>> x.isocalendar() (2016, 34, 1) >>> x.isoformat() '2016-08-22'
>>> from datetime import * >>> x = date.today() >>> x.weekday() 0 >>> x.isoweekday() 1
>>> from datetime import * >>> x = date.today() >>> x datetime.date(2016, 8, 22) >>> x.replace(month = 9) datetime.date(2016, 9, 22)
>>> from datetime import * >>> x = date.today() >>> x datetime.date(2016, 8, 22) # 今日は,2016/8/22 >>> x.toordinal() 736198 # 1/1/1 から 736198 日目 >>> date.fromordinal(736198) datetime.date(2016, 8, 22) # 1/1/1 から 736198 日目は,2016/8/22
>>> from datetime import * >>> x = date.today() >>> x.weekday() 0 >>> x.isoweekday() 1
>>> from datetime import * >>> x = time(minute = 10) >>> x datetime.time(0, 10) >>> x.minute 10
>>> from datetime import * >>> time(hour = 12, minute = 25).isoformat() '12:25:00'
>>> from datetime import * >>> x = time(hour = 12, minute = 25) >>> x.replace(minute = 20) datetime.time(12, 20)
>>> from datetime import * >>> datetime(2020, 9, 1, 12) - datetime(2016, 9, 1, 9) # 2020/9/1 12:00 - 2016/9/1 09:00 datetime.timedelta(1461, 10800) # 1461 日 10800 秒
>>> from datetime import * >>> x = datetime.today() >>> x datetime.datetime(2016, 8, 22, 19, 36, 42, 936212) >>> d = x.date() >>> d datetime.date(2016, 8, 22) >>> t = x.time() >>> t datetime.time(19, 36, 42, 936212) >>> datetime.combine(d, t) datetime.datetime(2016, 8, 22, 19, 36, 42, 936212)
>>> from datetime import * >>> x = datetime.today() >>> x datetime.datetime(2016, 8, 22, 19, 18, 28, 535617) # 今日は,2016/8/22 >>> x.toordinal() 736198 # 1/1/1 から 736198 日目 >>> datetime.fromordinal(736198) datetime.datetime(2016, 8, 22, 0, 0) # 1/1/1 から 736198 日目は,2016/8/22
>>> from datetime import * >>> datetime.today() datetime.datetime(2016, 8, 22, 19, 6, 20, 160684) >>> datetime.now() datetime.datetime(2016, 8, 22, 19, 6, 37, 611325) # 今日は,2016/8/22 19:06:37.611325
>>> from datetime import * >>> datetime.today() datetime.datetime(2016, 8, 22, 19, 6, 20, 160684) >>> datetime.now() datetime.datetime(2016, 8, 22, 19, 6, 37, 611325) # 今日は,2016/8/22 19:06:37.611325
>>> x = datetime.today() >>> x = datetime.today() >>> x datetime.datetime(2016, 8, 22, 19, 36, 42, 936212) >>> d = x.date() >>> d datetime.date(2016, 8, 22) >>> t = x.time() >>> t datetime.time(19, 36, 42, 936212) >>> datetime.combine(d, t) datetime.datetime(2016, 8, 22, 19, 36, 42, 936212)
>>> from datetime import * >>> x = datetime.today() >>> x.isocalendar() (2016, 34, 1) >>> x.isoformat() '2016-08-23T06:43:29.308330'
>>> from datetime import * >>> x = datetime.today() >>> x.isocalendar() (2016, 34, 1) >>> x.isoformat() '2016-08-23T06:43:29.308330'
>>> from datetime import * >>> x = datetime.today() >>> x.weekday() 0 >>> x.isoweekday() 1
>>> from datetime import * >>> x = datetime.today() >>> x datetime.datetime(2016, 8, 23, 6, 43, 29, 308330) >>> x.replace(day = 1) datetime.datetime(2016, 8, 1, 6, 43, 29, 308330)
>>> x = datetime.today() >>> x = datetime.today() >>> x datetime.datetime(2016, 8, 22, 19, 36, 42, 936212) >>> d = x.date() >>> d datetime.date(2016, 8, 22) >>> t = x.time() >>> t datetime.time(19, 36, 42, 936212) >>> datetime.combine(d, t) datetime.datetime(2016, 8, 22, 19, 36, 42, 936212)
>>> from datetime import * >>> x = datetime.today() >>> x datetime.datetime(2016, 8, 22, 19, 18, 28, 535617) # 今日は,2016/8/22 >>> x.toordinal() 736198 # 1/1/1 から 736198 日目 >>> datetime.fromordinal(736198) datetime.datetime(2016, 8, 22, 0, 0) # 1/1/1 から 736198 日目は,2016/8/22
>>> from datetime import * >>> x = datetime.today() >>> x.weekday() 0 >>> x.isoweekday() 1
class array.array(typecode[, initializer])
array.typecodes # クラス変数
>>> array("i", [1, 2, 3]) array('i', [1, 2, 3]) >>> typecodes 'bBuhHiIlLqQfd'
>>> A = [array("i", [1, 2, 3]), array("i", [4, 5, 6])] >>> A[0][2] 3
>>> from array import * >>> a = array("i", [1, 2, 3]) >>> a.typecode 'i' >>> a.itemsize 4
>>> from array import * >>> a = array("i", [1, 2, 3]) >>> a.append(0) >>> a array('i', [1, 2, 3, 0])
>>> from array import * >>> a = array("i", [1, 2, 1]) >>> a.count(1) 2
>>> from array import * >>> a = array("i", [1, 2, 3]) >>> a.extend([4, 5, 6]) >>> a array('i', [1, 2, 3, 4, 5, 6])
>>> from array import * >>> a = array("b", b"abc") >>> a.frombytes(b"efg") >>> a array('b', [97, 98, 99, 101, 102, 103]) >>> a.tobytes() b'abcefg'
>>> from array import * >>> a = array("d", [1.05, 2.25, 3.50]) >>> f = open("test.txt", "bw") >>> a.tofile(f) >>> f.close() >>> f = open("test.txt", "br") >>> b = array("d", [10, 20]) >>> b.fromfile(f, 3) >>> f.close() >>> b array('d', [10.0, 20.0, 1.05, 2.25, 3.5])
>>> from array import * >>> a = array("i", [1, 2, 3]) >>> a.fromlist([4, 5]) >>> a array('i', [1, 2, 3, 4, 5]) >>> a.tolist() [1, 2, 3, 4, 5]
>>> from array import * >>> a = array("i", [1, 2, 3, 2, 5]) >>> a.index(2) 1
>>> from array import * >>> a = array("i", [1, 2, 3]) >>> a.insert(1, 10) >>> a array('i', [1, 10, 2, 3])
>>> from array import * >>> a = array("i", [1, 2, 3, 2, 5]) >>> k = a.pop(1) >>> k, a (2, array('i', [1, 3, 2, 5])) >>> k = a.pop() >>> k, a (5, array('i', [1, 3, 2]))
>>> from array import * >>> a = array("i", [1, 2, 3, 2, 5]) >>> a.remove(2) >>> a array('i', [1, 3, 2, 5])
>>> from array import * >>> a = array("i", [1, 2, 3, 4, 5]) >>> a.reverse() >>> a array('i', [5, 4, 3, 2, 1])
情報学部 | 菅沼ホーム | 目次 | 索引 |