Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Pythonic #Python

遍历列表中的元素,并储存为变量

笨办法:

a = ['Admin', '127.0.0.1', '8080', 'maya_matthew_guaranty']
name = a[0]
server = a[1]
port = a[2]
password = a[3]

Pythonic:

a = ['Admin', '127.0.0.1', '8080', 'maya_matthew_guaranty']
name , server, port , password = a

Python 内置的 List vs Numpy

Test on MacBook Air (11-inch, Early 2015) 1.6 GHz Intel Core i5

List: List Time used: 7.607762999999999

import time
start = time.clock()
a = range(10000000)	
b = range(10000000)
c = []
for i in range(len(a)):
	c.append(a[i] + b[i])
elapsed = (time.clock() - start)
print("List Time used:",elapsed)

Numpy: Time used: 0.259793

import time
start = time.clock()
import numpy as np
a = np.arange(10000000)
b = np.arange(10000000)
c = a + b
elapsed = (time.clock() - start)
print("Time used:",elapsed)
# 当需要传递的参数很多时,打包成字典
myDict = {
'one': 1,
'two': 2,
'three': 3}
a, b, c = myDict.values()
@sobhanasasi

This comment has been minimized.

Copy link

@sobhanasasi sobhanasasi commented Dec 4, 2018

thanks for using time in above code it was usefull for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.