Skip to content

Instantly share code, notes, and snippets.

@ringsaturn
Last active June 2, 2022 04:20
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save ringsaturn/1e98905847e6c3ef7d7b09e4af8942be to your computer and use it in GitHub Desktop.
Save ringsaturn/1e98905847e6c3ef7d7b09e4af8942be to your computer and use it in GitHub Desktop.
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
Copy link

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