Difference between revisions of "CSC111 Lab 8 2014"
(→Splitting Strings) |
(→Splitting Strings) |
||
Line 48: | Line 48: | ||
>>> " ".join( newWords ) <font color="magenta"># join strings in newWords list with a space</font> | >>> " ".join( newWords ) <font color="magenta"># join strings in newWords list with a space</font> | ||
+ | |||
+ | ==Mini Assignments== | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge 1== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * Use a judicious mix() of split() and join operations to convert the string | ||
+ | |||
+ | "1 China 1,339,190,000 9,596,960.00 139.54 3,705,405.45 361.42" | ||
+ | |||
+ | :into a new string: | ||
+ | |||
+ | "China 1339190000" | ||
+ | |||
+ | :Note that this line is taken from a table from this [http://www.worldatlas.com/aatlas/populations/ctypopls.htm URL] where the numbers after the country indicate a) the population, the area and population density expressed with square-kilometers, and the area and population density expressed with square-miles. | ||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | |||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | {| style="width:100%; background:silver" | ||
+ | |- | ||
+ | | | ||
+ | ==Challenge x== | ||
+ | |} | ||
+ | [[Image:QuestionMark1.jpg|right|120px]] | ||
+ | |||
+ | * | ||
+ | |||
+ | |||
+ | <br /> | ||
+ | <br /> | ||
+ | <!-- ----------------------------------------------------------------------------------------------- --> | ||
+ | * Figure out a way to take a string of the form "Pakistan 108 166 226" where the first word is a country name, and the following three numbers are estimated populations of this country in 1900, 2008, and 2025, into a new string with only the first and last words, i.e. "Pakistan 226". | ||
+ | |||
+ | |||
+ | China 1,458 | ||
+ | India 1,398 | ||
+ | United-States 352 | ||
+ | Indonesia 273 | ||
+ | Brazil 223 | ||
+ | Pakistan 226 | ||
+ | Bangladesh 198 | ||
+ | Nigeria 208 | ||
+ | Russia 137 | ||
+ | Japan 126 |
Revision as of 14:36, 24 March 2014
--D. Thiebaut (talk) 14:01, 24 March 2014 (EDT)
This lab deals with strings and list operations, and transforming strings into lists and lists into strings.
Contents
Splitting Strings
Work in the console, and try these different commands. Observe what the different operations do.
>>> line = "The quick, red fox jumped. It jumped over the lazy, sleepy, brown dog." >>> line >>> line.split() >>> words = line.split() >>> words >>> words[0] >>> words[1] >>> words[-1] >>> words[-2] >>> chunks = line.split( ',' ) # split on commas >>> chunks >>> chunks = line.split( '.' ) # split on periods >>> chunks >>> words >>> separator = "+" >>> newLine = separator.join( words ) # join the words into a new string and use separator as the glue >>> newLine >>> separator = "$$$" >>> newLine = separator.join( words ) # same but use $$$ as the glue >>> newLine >>> words # verify that you still have individual words in this list >>> newWords = [ words[0], words[3], words[4], words[7], words[8], words[12] ] # create a new list >>> newWords >>> " ".join( newWords ) # join strings in newWords list with a space
Mini Assignments
Challenge 1 |
- Use a judicious mix() of split() and join operations to convert the string
"1 China 1,339,190,000 9,596,960.00 139.54 3,705,405.45 361.42"
- into a new string:
"China 1339190000"
- Note that this line is taken from a table from this URL where the numbers after the country indicate a) the population, the area and population density expressed with square-kilometers, and the area and population density expressed with square-miles.
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
Challenge x |
- Figure out a way to take a string of the form "Pakistan 108 166 226" where the first word is a country name, and the following three numbers are estimated populations of this country in 1900, 2008, and 2025, into a new string with only the first and last words, i.e. "Pakistan 226".
China 1,458
India 1,398
United-States 352
Indonesia 273
Brazil 223
Pakistan 226
Bangladesh 198
Nigeria 208
Russia 137
Japan 126