Using python3:
https://stringpiggy.hpd.io/mac-osx-python3-dual-install/
==================Hackerank log==================
(0) range()
Notice that when we call range(n), the return is not a list [0,1,2,3,...,n]. It is a range object
spam = range(10)
spam[4] = -1
>> ERROR
spam = list(range(10)) spam[4] = -1
>> OK
(1) Append vs extend??x = [1, 2, 3]
x.append([4, 5])
print (x)
====>[1, 2, 3, [4, 5]]
x = [1, 2, 3]
x.extend([4, 5])
print (x)
====>
[1, 2, 3, 4, 5]
(1) Any
See:https://www.hackerrank.com/challenges/string-validators/problem
print 'ab123'.isalnum() # check if all element is alphabet or number
>>> True
print 'ab123#'.isalnum()
>>> False
If we wanna check there is ANY element if
there is alphabet or number
c =
'ab123#'
print(any(c.isalnum() for c in s))
>>> True
(2) Set:https://blog.csdn.net/business122/article/details/7541486
Copy from a set (if you use b=c, modify on b will affect c)
we have to copy element by element
(Of course you can use c = b.copy())
>>> b
set([33, 11, 44, 22])
>>> c = [i for i in b]
>>> c
[33, 11, 44, 22]
Method: Add(add a element)/ Update(add multiple element)/
Remove(delete one element)/ Discard(delete one element if exist)/
intersection/ union/ difference
A good example comes from here:
https://www.hackerrank.com/challenges/find-second-maximum-number-in-a-list/problem
on line to represent second big element in an unsorted array
Given list is . The maximum score is , second maximum is
print sorted(list(set(nums)))[-2]
https://www.hackerrank.com/challenges/nested-list/forum
python
students = [['Harry', 37.21], ['Berry', 37.21], ['Tina', 37.2], ['Akriti', 41], ['Harsh', 39]]
The lowest grade of belongs to Tina. The second lowest grade of belongs to both Harry and Berry,
for _ in range(int(input())):
name = input()
score = float(input())
table.append([name,score])
s = set([score for (name, score) in table])
#print(s)
{41.0, 37.2, 37.21, 39.0}
s = sorted(list(s))
#print(s)
[37.2, 37.21, 39.0, 41.0]
second_lowest_score = s[1]
for (name, score) in sorted(table):
if score == second_lowest_score:
print(name) #
Berry Harry
(3) map/ filter/ reduce
https://blog.csdn.net/seetheworld518/article/details/46959871
notice that map in python is totally not the same thing in C++
map in C++ in hash map, map in python means mapping a specific function to a given sequence
Example:
# Lambda
print map(lambda x: x % 2, range(7)) #[0,1,2,3,4,5,6]
>>>
[0, 1, 0, 1, 0, 1, 0]
# When your function involve multiple sequence
print map(lambda x , y : x ** y, [2,4,6],[3,2,1]) #2^3 4^2 6^1
>>>
[8, 16, 6]
# Another
print map(lambda x , y : (x ** y, x + y), [2,4,6],[3,2,1])
>>> [(8, 5), (16, 6), (6, 7)]
# If not assign function, it become Zip()
print map(None, [2,4,6],[3,2,1])
>>> [(2, 3), (4, 2), (6, 1)]
# Other example to transfer into list
map(int, (1,2,3))
>>> [1, 2, 3]
# Extract key from diction
map(int, {1:2,2:3,3:4})
[1, 2, 3]
# Apply customize function
def u_to_l (s):
return s.upper()
print map(u_to_l,'asdfd')
(4)zip
(5) output format (python3)
print(summ)
>> 56.00
print("{0:.2f}".format(summ))
>> 56.0
(6)list operation
https://docs.python.org/2/tutorial/datastructures.html
list.append(x)/ # list.extend(x): add x to end of list
list.insert(i,x) # insert element x at index i
list.remove(x) # erase element of value x
e = list.pop(i) # erase element of index i and return
i = list.index(x)# return the index of element x
c = list.count(x)# return the number outcome of x in list
list.sort() # = sorted(list), in-place
list.reverse() # reverse, in-place
(7)String
String in python can not modify the value in-place. See example:
https://www.hackerrank.com/challenges/python-mutations/problem
We can choose (a)convert to list and convert back (b)attach sub-string
Example:
spam = 'I have a pet cat.'
spam[13] = 'r'
print(spam)
>> ERROR
spam = 'I have a pet cat.'
li = list(
spam
)
li[13] = 'r'
spam
= ''.join(li)>>> 'I have a pet rat.'
spam = 'I have a pet cat.' spam = spam[:13] + 'r' + spam[14:] print(spam)
>>> 'I have a pet rat.'
Split/ Join: separate string to list/ combine string from list
li = ['my','name','is','bob']
' '.join(li)
>>> 'my name is bob'
'..'.join(s)
>>> 'my..name..is..bob'
b = 'my..name..is..bob'
b.split("..")
>>> ['my', 'name', 'is', 'bob']
b.split("..",1)
>>> ['my', 'name..is..bob']
Replace:
http://www.runoob.com/python/att-string-replace.html
see: https://www.hackerrank.com/challenges/capitalize/problem
If you convert it to list and capitalize and convert back, you will lost space
Use replace instead
s = string for x in s.split(): s = s.replace(x, x[0].upper()+x[1:]) #print(s) return s
==================Machine Learning log==================
(histogram() and plot())
install pandas and matplotlib$pip3 install pandas
$pip3 install matplotlib
use in code:
import pandas
from pandas.plotting import scatter_matrix
import matplotlib.pyplot as plt
list and matrix
http://blog.hhjh.tn.edu.tw/biosomeday/?p=655
array = [0] * attr_number
matrix = [array]*attr_number
print(matrix)
matrix[1][1] = 1
> [0, 1, 0, 0]
[0, 1, 0, 0]
[0, 1, 0, 0]
[0, 1, 0, 0]
use this instead https://www.cnblogs.com/woshare/p/5823303.html
matrix = [[-1 for i in range(attr_number)] for i in range(attr_number)]
matrix[1][1] = 1
> [0, 0, 0, 0]
[0, 1, 0, 0]
[0, 0, 0, 0]
[0, 0, 0, 0]
read file and split:
http://1234.logdown.com/posts/260821-python-split
==================Machine Learning log2==================1.
Cpp 中常用的 vector.find(),在python中可以:
3 in [1, 2, 3] # => True
或是
context = ['1','2','?']
if ('?' in context):
print("got '?'")
2. 需要sorting 一個array,找出前k個在原本array的順序時:>>> s = [2, 3, 1, 4, 5]
>>> sorted(range(len(s)), key=lambda k: s[k])
[2, 0, 1, 3, 4]
>>>
ps. 預設降幂排序,想要升幂:
sorted(range(len(s)),
reverse=True)
3. 想要把list中的數字同除一個分母:
myList[:] = [x / myInt for x in myList]
4. 將list 隨機排序:
import random random.shuffle (lst )
Machine Learning log31. 從list當中隨機選擇k個index:
可以重複:
np.random.choice(150, k)
不重複:
random.sample(range(150), 10)
沒有留言:
張貼留言