table = [[2, 3, 2, 5],
[1, 2, 5, 4],
[4, 3, 5, 4],
[5, 2, 3, 1]]
# Задача: отсортировать таблицу table
# по второму столбцу, а при совпадении элементов во втором
# столбце — по первому
def sort_2_then_1(table):
def my_sort_key(element):
return (element[1], element[0])
# return element[1::-1]
return sorted(table, key=my_sort_key)
### YOUR SOLUTION HERE
assert sort_2_then_1(table) == [
[1, 2, 5, 4],
[5, 2, 3, 1],
[2, 3, 2, 5],
[4, 3, 5, 4]]
sorted(['hello', 'world', 'test'], key=len)
['test', 'hello', 'world']
def sort_2_then_1(table):
return sorted(table,
key=lambda element: (element[1], element[0]))
sort_2_then_1(table)
[[1, 2, 5, 4], [5, 2, 3, 1], [2, 3, 2, 5], [4, 3, 5, 4]]
(lambda x, y=2: x + y)(3, 4)
7
incrementors = []
for i in range(10):
incrementors.append(lambda x: x + i)
incrementors[5](3)
12
i = 100
incrementors[5](3)
103
incrementors = []
for i in range(10):
incrementors.append(lambda x, i=i: x + i)
incrementors[3](1)
4
def foo(x, *args):
print("x =", x)
print("args =", args)
foo(1, 2, 3)
x = 1 args = (2, 3)
def bar(x, **kwargs):
print("x =", x)
print("kwargs =", kwargs)
bar(x=5, y=7, z=8)
x = 5 kwargs = {'y': 7, 'z': 8}
import matplotlib.pyplot as plt
%matplotlib inline
points = [(1, 2), (2, 7), (1, 5), (2, 4)]
# plt.plot([1, 2, 1, 2], [2, 7, 5, 4])
for x, y in points:
print(x)
1 2 1 2
[x ** 2 for x in range(10)]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
def myplot(points, **kwargs):
plt.plot([x for x, y in points],
[y for x, y in points], **kwargs)
def foo(x, y):
return 2 * x + y
foo(x=4, y=5)
13
foo(**{'x': 4, 'y': 5})
13
myplot([(x ** 3, x ** 2) for x in range(-5, 6)],
color='teal', lw=4, marker='*')
x
2
locals()['x']
2
import os
os.getcwd()
'/Users/user/prj/oldhse-2010-11/repo/2019-20/nes-datascience'
!ls
Lecture 1.ipynb Untitled1.ipynb lecture 4.ipynb ps01-6.ipynb Lecture 2.ipynb assignments lecture 5.ipynb test.txt Untitled.ipynb lecture 03.ipynb old
#!dir for windows
f = open("test.txt")
for line in f:
print(repr(line))
f.close()
'Раз два три четыре пять\n' 'Вышел зайчик погулять\n' 'One two three\n' '\n' 'This is my last line.'
f = open("test.txt")
for line in f:
print(line.rstrip())
f.close()
print("Okay")
Раз два три четыре пять Вышел зайчик погулять One two three This is my last line. Okay
f = open("test.txt")
for line in f:
print(line, end="")
f.close()
print("Okay")
Раз два три четыре пять Вышел зайчик погулять One two three This is my last line.Okay
f = open("test.txt")
for line in f:
print(line.rstrip())
print("Once again")
f.close()
f = open("test.txt")
for line in f:
print(line.rstrip())
f.close()
Раз два три четыре пять Вышел зайчик погулять One two three This is my last line. Once again Раз два три четыре пять Вышел зайчик погулять One two three This is my last line.
a = [1, 2, 3]
for i in a:
print(i)
for i in a:
print(i)
1 2 3 1 2 3
f = open("test.txt")
for line in f:
print(line.rstrip())
print("Once again")
f.seek(0)
for line in f:
print(line.rstrip())
f.close()
Раз два три четыре пять Вышел зайчик погулять One two three This is my last line. Once again Раз два три четыре пять Вышел зайчик погулять One two three This is my last line.
f = open("test.txt")
text = f.read()
f.close()
text
'Раз два три четыре пять\nВышел зайчик погулять\nOne two three\n\nThis is my last line.'
f = open("test.txt")
lines = f.readlines()
f.close()
lines
['Раз два три четыре пять\n', 'Вышел зайчик погулять\n', 'One two three\n', '\n', 'This is my last line.']
with open('test.txt') as f:
for line in f:
print(line.rstrip())
print(f.closed)
print(f.closed)
Раз два три четыре пять Вышел зайчик погулять One two three This is my last line. False True
f.read()
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-84-571e9fb02258> in <module>() ----> 1 f.read() ValueError: I/O operation on closed file.
with open('test-new.txt', encoding='cp1251') as f:
for line in f:
print(line.rstrip())
Раз два три четыре пять Вышел зайчик погулять One two three This is my last line.