-1

リストに 1 つまたは複数の要素を追加する必要があり、次の 2 つのオプションがあります。

mylist=mylist+newlistまた(elemet)

またmylist.append(ele);

どちらが速くなりますか?

4

1 に答える 1

1

mylist.append(ele)より速くなります。これは Python Docs に記載されています。

ドキュメントからの引用 -

The method append() shown in the example is defined for list objects; it adds a new element at the end of the list. In this example it is equivalent to result = result + [a], but more efficient.

myList = myList + something新しいリストを作成して再割り当てする必要があります。

結果を比較してくださいtimeit-

>>> timeit('myList = myList + ["a"]', 'myList = []', number = 50000)
11.35058911138415
>>> timeit('myList.append("a")', 'myList = []', number = 50000)
0.010776052286637139
于 2013-08-04T09:14:02.637 に答える