dketov
12/11/2011 - 7:57 PM

Список, часть 1

Список, часть 1

# -*- encoding: utf-8 -*-
"""
Удаление элемента
"""

li = ['a', 'b', 'new', 'mpilgrim', 'z', 'example', 'new', 'two', 'elements'] 

print li.remove("z")                                     
print li 

li.remove("new")                                   
print li 

li.remove("z")                                    

print li 
# -*- encoding: utf-8 -*-
"""
Вставка элементов
"""

a = ['spam', 'eggs', 100, 1234]
# Insert some:
a[1:1] = ['bletch', 'xyzzy']
print a

a[:0] = a     # Insert (a copy of) itself at the beginning
print a
# -*- encoding: utf-8 -*-
"""
Удаление среза
"""

a = ['spam', 'eggs', 100, 1234]
# Remove some:
a[0:2] = []
print a
# -*- encoding: utf-8 -*-
"""
Срезы списка
"""

numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
print numbers[3:6]

print numbers[0:1]

print numbers[7:10]

print numbers[-3:-1]

print numbers[-3:]

print numbers[:3]

print numbers[:]
# -*- encoding: utf-8 -*-
"""
Длина списка
"""

a = ['spam', 'eggs', 100, 1234]
print len(a)
# -*- encoding: utf-8 -*-
"""
Разница между добавлением и расширением
"""

li = ['a', 'b', 'c'] 
li.extend(['d', 'e', 'f'])                         
print li 

print len(li)                                            

print li[-1] 

li = ['a', 'b', 'c'] 
li.append(['d', 'e', 'f'])                        

print li 

print len(li)                                            

print li[-1] 
# -*- encoding: utf-8 -*-
"""
Добавление элементов
"""

scores = ["1","2","3"]

# add a score
score = int(raw_input("What score did you get?: "))
scores.append(score)

# list high-score table
for score in scores:
       print score

# -*- encoding: utf-8 -*-
"""
Присваивание
"""

aList = [123, 'abc', 4.56, ['inner', 'list'], 7-9j]
anotherList = [None, 'something to see here']
print aList
print anotherList
aListThatStartedEmpty = []
print aListThatStartedEmpty
print list('foo')
# -*- encoding: utf-8 -*-
"""
Определение
"""

a = ['spam', 'eggs', 100, 1234]
print a