Differences

This shows you the differences between two versions of the page.

part1 [2010/01/24 13:06]
julia
part1 [2010/01/24 13:51] (current)
julia
Line 364: Line 364:
Remember that lists are mutable, so the removed item is lost. We can also remove any other in the list, let's say 'C'. First, we reassign the original list items and then remove the second item Remember that lists are mutable, so the removed item is lost. We can also remove any other in the list, let's say 'C'. First, we reassign the original list items and then remove the second item
-<code python>nucleotides = [ 'A', 'C', 'G'. 'T']+<code python>nucleotides = [ 'A', 'C', 'G', 'T']
nucleotides.pop(1)</code> nucleotides.pop(1)</code>
Line 375: Line 375:
Shifting from our 'destructive' mode, we cal also add elements to the list. Adding to the end of the list is trivial, by using <code>append</code> Shifting from our 'destructive' mode, we cal also add elements to the list. Adding to the end of the list is trivial, by using <code>append</code>
-<code python>nucleotides = [ 'A', 'C', 'G'. 'T']+<code python>nucleotides = [ 'A', 'C', 'G', 'T']
nucleotides.append('A')</code> nucleotides.append('A')</code>
that returns that returns
-<code python>nucleotides = [ 'A', 'C', 'G'. 'T', 'A']</code>+<code python>nucleotides = [ 'A', 'C', 'G', 'T', 'A']</code>
Adding to any position is also very straightforward with <code>insert</code>, like this Adding to any position is also very straightforward with <code>insert</code>, like this
-<code python>nucleotides = [ 'A', 'C', 'G'. 'T']+<code python>nucleotides = [ 'A', 'C', 'G', 'T']
nucleotides.insert(0, 'A')</code> nucleotides.insert(0, 'A')</code>
where <code>insert</code> takes two arguments: first is the index of the element before which to insert and second the element to be inserted. So our line above will insert an 'A' just before the 'A' at position zero. We can try this where <code>insert</code> takes two arguments: first is the index of the element before which to insert and second the element to be inserted. So our line above will insert an 'A' just before the 'A' at position zero. We can try this
-<code python>nucleotides = [ 'A', 'C', 'G'. 'T']+<code python>nucleotides = [ 'A', 'C', 'G', 'T']
nucleotides.insert(0, 'A1') nucleotides.insert(0, 'A1')
nucleotides.insert(2, 'C1') nucleotides.insert(2, 'C1')
Line 403: Line 403:
And for last, we will take care of the output. Of course if are creating a script that requires a nicer output, printing a list is not the best way. We could create a loop and merge all entries in the list, but that would be a couple of lines and we ought to have an easier way (otherwise we could be using C++ instead). There is a way, by using the method <code>join</code>. This method will join all the elements in a list into a single string, with a selected delimiter. And for last, we will take care of the output. Of course if are creating a script that requires a nicer output, printing a list is not the best way. We could create a loop and merge all entries in the list, but that would be a couple of lines and we ought to have an easier way (otherwise we could be using C++ instead). There is a way, by using the method <code>join</code>. This method will join all the elements in a list into a single string, with a selected delimiter.
-<code python>nucleotides = [ 'A', 'C', 'G'. 'T']+<code python>nucleotides = [ 'A', 'C', 'G', 'T']
"".join(nucleotides)</code> "".join(nucleotides)</code>
 
part1.txt · Last modified: 2010/01/24 13:51 by julia
 
Except where otherwise noted, content on this wiki is licensed under the following license:CC Attribution-Noncommercial-Share Alike 3.0 Unported
Recent changes RSS feed Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki