Created
December 11, 2011 20:02
-
-
Save dketov/1462429 to your computer and use it in GitHub Desktop.
Список, часть 2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Двумерный список | |
""" | |
matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] | |
print matrix[1] | |
[4, 5, 6] | |
print matrix[1][1] | |
print matrix[2][0] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Конкатенация списков | |
""" | |
num_list = [43, -1.23, -2, 6.19e5] | |
str_list = ['jack', 'jumped', 'over', 'candlestick'] | |
mixup_list = [4.0, [1, 'x'], 'beef', -1.9+6j] | |
print num_list + mixup_list | |
print str_list + num_list |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Сравнение списков | |
""" | |
L1 = [1, ('a', 3)] # same value, unique objects | |
L2 = [1, ('a', 3)] | |
print L1 == L2, L1 is L2 # equivalent?, same object? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Генератор списка | |
""" | |
li = ["a", "mpilgrim", "foo", "b", "c", "b", "d", "d"] | |
print [elem for elem in li if len(elem) > 1] | |
print [elem for elem in li if elem != "b"] | |
print [elem for elem in li if li.count(elem) == 1] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- encoding: utf-8 -*- | |
""" | |
Генератор списка (расширенный пример) | |
""" | |
# List comprehensions provide a concise way to create lists without resorting to use | |
# of map(), filter() and/or lambda. | |
freshfruit = [' banana', ' loganberry ', 'passion fruit '] | |
print [weapon.strip() for weapon in freshfruit] | |
vec = [2, 4, 6] | |
print [3*x for x in vec] | |
print [3*x for x in vec if x > 3] | |
print [3*x for x in vec if x < 2] | |
print [[x,x**2] for x in vec] | |
print [(x, x**2) for x in vec] | |
vec1 = [2, 4, 6] | |
vec2 = [4, 3, -9] | |
print [x*y for x in vec1 for y in vec2] | |
print [x+y for x in vec1 for y in vec2] | |
print [vec1[i]*vec2[i] for i in range(len(vec1))] | |
print [str(round(355/113.0, i)) for i in range(1,6)] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment