In [1]:
list = []
In [3]:
list2 = [1, 'hello world', True, list]
In [4]:
list2
Out[4]:
[1, 'hello world', True, []]
In [5]:
list3 = [1,2,3]
In [6]:
list3 + list2
Out[6]:
[1, 2, 3, 1, 'hello world', True, []]
In [7]:
len(list3)
Out[7]:
3
In [8]:
list3 * 2
Out[8]:
[1, 2, 3, 1, 2, 3]
In [9]:
list3[0]
Out[9]:
1
In [10]:
list3[-1]
Out[10]:
3
In [11]:
list3
Out[11]:
[1, 2, 3]
In [12]:
list3[0] = 10
In [13]:
list3
Out[13]:
[10, 2, 3]
In [14]:
a = list3
In [15]:
a
Out[15]:
[10, 2, 3]
In [16]:
b = a
In [17]:
b
Out[17]:
[10, 2, 3]
In [18]:
a[1] = 20
In [19]:
a
Out[19]:
[10, 20, 3]
In [20]:
b
Out[20]:
[10, 20, 3]
In [21]:
c = a.copy()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-21-37d27e6e7a84> in <module>()
----> 1 c = a.copy()

AttributeError: 'list' object has no attribute 'copy'
In [22]:
dir(a)
Out[22]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__delslice__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getslice__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__setslice__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']
In [23]:
c = a[:]
In [24]:
a
Out[24]:
[10, 20, 3]
In [25]:
c
Out[25]:
[10, 20, 3]
In [26]:
a[-1] = 30
In [27]:
a
Out[27]:
[10, 20, 30]
In [28]:
c
Out[28]:
[10, 20, 3]
In [29]:
[1,2,3] == [1,3,2]
Out[29]:
False
In [30]:
list4 = [0,1,2,3,4,5]
In [31]:
list4[0:2]
Out[31]:
[0, 1]
In [32]:
1 in list4
Out[32]:
True
In [33]:
10 in list4
Out[33]:
False
In [34]:
list4
Out[34]:
[0, 1, 2, 3, 4, 5]
In [35]:
list4.append(6)
In [36]:
list4
Out[36]:
[0, 1, 2, 3, 4, 5, 6]
In [37]:
list4.pop()
Out[37]:
6
In [38]:
list4
Out[38]:
[0, 1, 2, 3, 4, 5]
In [39]:
list4.pop(0)
Out[39]:
0
In [40]:
list4
Out[40]:
[1, 2, 3, 4, 5]
In [41]:
x = list4.append(6)
In [42]:
x
In [43]:
list4
Out[43]:
[1, 2, 3, 4, 5, 6]
In [44]:
y = list4.pop()
In [45]:
y
Out[45]:
6
In [46]:
y
Out[46]:
6
In [47]:
del y
In [48]:
y
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-48-009520053b00> in <module>()
----> 1 y

NameError: name 'y' is not defined
In [49]:
list4
Out[49]:
[1, 2, 3, 4, 5]
In [50]:
del list4[-1]
In [51]:
list4
Out[51]:
[1, 2, 3, 4]
In [52]:
dir(list4)
Out[52]:
['__add__',
 '__class__',
 '__contains__',
 '__delattr__',
 '__delitem__',
 '__delslice__',
 '__doc__',
 '__eq__',
 '__format__',
 '__ge__',
 '__getattribute__',
 '__getitem__',
 '__getslice__',
 '__gt__',
 '__hash__',
 '__iadd__',
 '__imul__',
 '__init__',
 '__iter__',
 '__le__',
 '__len__',
 '__lt__',
 '__mul__',
 '__ne__',
 '__new__',
 '__reduce__',
 '__reduce_ex__',
 '__repr__',
 '__reversed__',
 '__rmul__',
 '__setattr__',
 '__setitem__',
 '__setslice__',
 '__sizeof__',
 '__str__',
 '__subclasshook__',
 'append',
 'count',
 'extend',
 'index',
 'insert',
 'pop',
 'remove',
 'reverse',
 'sort']
In [53]:
sorted(list4)
Out[53]:
[1, 2, 3, 4]
In [54]:
list4
Out[54]:
[1, 2, 3, 4]
In [55]:
list5 = [1,3,2,5,4]
In [56]:
sorted(list5)
Out[56]:
[1, 2, 3, 4, 5]
In [57]:
list5
Out[57]:
[1, 3, 2, 5, 4]
In [58]:
list5.sort()
In [59]:
list5
Out[59]:
[1, 2, 3, 4, 5]
In [60]:
list5 = [1,3,2,5,4]
In [61]:
sorted(list5, reverse=True)
Out[61]:
[5, 4, 3, 2, 1]
In [62]:
list6 = ['a', 'aaa', 'aa']
In [65]:
sorted(list6, key=len)
Out[65]:
['a', 'aa', 'aaa']
In [66]:
sorted(list6, key=len, reverse=True)
Out[66]:
['aaa', 'aa', 'a']
In [67]:
list6
Out[67]:
['a', 'aaa', 'aa']
In [68]:
list7 = ['aa', 'b', 'ccc']
In [69]:
';'.join(list7)
Out[69]:
'aa;b;ccc'
In [70]:
x = ';'.join(list7)
In [71]:
x
Out[71]:
'aa;b;ccc'
In [72]:
x.split(';')
Out[72]:
['aa', 'b', 'ccc']
In [73]:
# tuple
tuple1 = ()
tuple2 = (1, 3, 5)
In [74]:
tuple2[0] = 10
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-74-a73b6f46c500> in <module>()
----> 1 tuple2[0] = 10

TypeError: 'tuple' object does not support item assignment
In [75]:
(x, y) = (1, 3)
In [76]:
x
Out[76]:
1
In [77]:
y
Out[77]:
3
In [80]:
list1 = (1, 2, 'hello', tuple2)
In [81]:
list1
Out[81]:
(1, 2, 'hello', (1, 3, 5))
In [84]:
list1[-1][0] = 10
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-84-a1fa469c22d9> in <module>()
----> 1 list1[-1][0] = 10

TypeError: 'tuple' object does not support item assignment
In [87]:
list(tuple2)
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-87-16d455d58089> in <module>()
----> 1 list(tuple2)

TypeError: 'list' object is not callable
In [86]:
tuple2
Out[86]:
(1, 3, 5)
In [88]:
list
Out[88]:
[]
In [89]:
del list
In [90]:
list(tuple2)
Out[90]:
[1, 3, 5]

file stream

In [91]:
!pwd
/Users/Megaroutte

In [92]:
! echo "Line1: Hello World" > data1.txt
In [93]:
! echo "Line2: Hello people" >> data1.txt
In [94]:
! echo "Line2: Hello you" >> data1.txt
In [96]:
!cat data1.txt
Line1: Hello World
Line2: Hello people
Line2: Hello you

In [97]:
fp = open('data1.txt')
In [98]:
for line in fp:
    print line
Line1: Hello World

Line2: Hello people

Line2: Hello you


In [100]:
fp = open('data1.txt')
for line in fp:
    print line,
Line1: Hello World
Line2: Hello people
Line2: Hello you

In [101]:
fp = open('data1.txt')
line_list = fp.readlines()
In [102]:
line_list
Out[102]:
['Line1: Hello World\n', 'Line2: Hello people\n', 'Line2: Hello you\n']
In [103]:
fp = open('data1.txt')
string1 = fp.read()
In [104]:
string1
Out[104]:
'Line1: Hello World\nLine2: Hello people\nLine2: Hello you\n'
In [105]:
string1.count('Hello')
Out[105]:
3
In [107]:
fw = open('data2.txt', 'w')
In [108]:
!cat data1.txt
In [113]:
! echo "Line1: Hello World" > data1.txt
In [114]:
! echo "Line1: Hello People" >> data1.txt
In [115]:
! echo "Line1: Hello You" >> data1.txt
In [116]:
cat data1.txt
Line1: Hello World
Line1: Hello People
Line1: Hello You

In [117]:
string1 = open('data1.txt').read()
In [118]:
fw
Out[118]:
<open file 'data2.txt', mode 'w' at 0x103814660>
In [122]:
fw.write(string1)
fw.close()
In [123]:
!cat data2.txt
Line1: Hello World
Line1: Hello People
Line1: Hello You
Line1: Hello World
Line1: Hello People
Line1: Hello You

In [121]:
string1
Out[121]:
'Line1: Hello World\nLine1: Hello People\nLine1: Hello You\n'
In [124]:
fw = open('data2.txt', 'w')
string2 = 'This is a string'
In [126]:
fw.write(string2)
fw.close()
In [127]:
!cat data2.txt
This is a stringThis is a string

Exercise:

In [128]:
l = ['', 'xyzzz', 'aabddda', 'xyx', 'bbbb']
for i in l:
    if len(i) > 2 and i[0] == i[-1]:
        print i
aabddda
xyx
bbbb

In [129]:
# 2) remove the duplicates in the following list
l = [1,2,2,3,3,5,4,1,1,1]
new_list = []
for i in l:
    if not i in new_list:
        # add it
        new_list.append(i)
        
print new_list
        
  File "<ipython-input-129-e158a603ae20>", line 8
    print new_list
                  ^
IndentationError: expected an indented block
In [132]:
!curl http://lyorn.idyll.org/~gjr/public2/swc/1k.taxonomy -O
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100  102k  100  102k    0     0  3207k      0 --:--:-- --:--:-- --:--:-- 3302k

In [133]:
# collect unique
unique_list = []
# collect all
all_list = []
for line in open('1k.taxonomy'):
    # +++add your code+++
    name, taxon = line.rstrip().split('\t')
    domain = taxon.split(';')[0]
    all_list.append(domain)
    if not domain in unique_list:
        unique_list.append(domain)

# count unique, hint: list.count
# +++add your code+++
for i in unique_list:
    count = all_list.count(i)
    print i, count
Bacteria 956
Eukaryota 19
Archaea 25

In [134]:
pwd
Out[134]:
u'/Users/Megaroutte'
In []: