Best Friends: Strings and Lists

damjaeng-i·2022년 8월 7일
0

2022 PY4E

목록 보기
14/18
post-thumbnail

Concatenating lists using +

We can create a new list by adding two existing lists together

>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> c = a + b
>>> print(c)
[1, 2, 3, 4, 5, 6]
>>> print(a)
[1, 2, 3]

Lists can be sliced using:

>>> t = [9, 41, 12, 3, 74, 15]
>>> t[1:3]
[41, 12]
>>> t[:4]
[9, 41, 12, 3]
>>> t[3:]
[3, 74, 15]
>>> t[:]
[9, 41, 12, 3, 74, 15]

Remember: Just like in strings, the second number is “up to but not including”

List Methods

>>> x = list()
>>> type(x)
<type 'list'>
>>> dir(x)
['append', 'count', 'extend', 'index', 'inser', 'pop', 'remove', 
'reverse', 'sor']

Data Structures - Python 3.10.6 documentation

https://docs.python.org/3/tutorial/datastructures.html

Building a List from Scratch

  • We can create an empty list and then add elements using the append method
  • The list stays in order and new elements are added at the end of the list
>>> stuff = list()
>>> stuff.append('book')
>>> studd.append(99)
>>> print(stuff)
['book', 99]
>>> stuff.append('cookies')
>>> print(stuff)
['book', 99, 'cookie']

Is Something in a List?

  • Python provides two operators that let you check if an item is in a list
  • These are logical operators that return True or False
  • They do not modify the list
>>> some = [1, 9, 21, 10, 16]
>>> 9 in some
True
>>> 15 in some
False
>>> 20 not in some
True

Lists are in Order

  • A list can hold many items and keeps those items in the order until we do something to change the order
  • A list can be sorted (i.e., change its order)
  • The sort method (unlike in strings) means “sort yourself”
>>> friends = ['Joseph', 'Glenn', 'Sally']
>>> friends.sort()
>>> print(friends)
['Glenn', 'Joseph', 'Sally'] 
Joseph 

Strings are immutable and lists are mutable.

Built-in Fuctions and Lists

  • There are a number of functions built into Python that take lists as parameters.
  • Remember the loops we built? These are much simpler.
>>> nums = [3, 41, 12, 9, 74, 15]
>>> print(len(nums))
6
>>> print(max(nums))
74
>>> print(min(nums))
3
>>> print(sum(nums)) 
154
>>> print(sum(nums)/len(nums)) 
25.6

Getting Average using Lists

numlist = list()
while True:
		inp = input('Enter a number: ')
		if inp == 'done' : break
		value = float(inp)
		numlist.append(value)

average = sum(numlist) / len(numlist)
print('Average:', average)

It uses more memory than using 'Total'.


Best Friends: Strings and Lists

>>> abc = 'With three words'
>>> stuff = abc.split()
>>> print(stuff)
['With', 'three', 'words']
>>> print(len(stuff))
3
>>> print(stuff[0])
With 

Split breaks a string into parts and produces a list of strings. We think of these as words. We can access a particular word or loop through all the words.

>>> line = 'A lot                of spaces'
>>> etc = line.split()
>>> print(etc)
['A', 'lot', 'of', 'spaces']
>>> 
>>> line = 'first;second;third'
>>> thing = line.split()
>>> print(thing)
['first;second;third']
>>> print(len(thing))
1
>>> thing = line.split(';')
>>> print(thing)
['first', 'second', 'third']
>>> print(len(thing))
3
  • When you do not specify a delimiter, multiple spaces are treated like one delimiter
  • You can specify what delimiter character to use in the splitting

Day Paring

From stephen.marquard@uct.ac.za Sat Jan 5:09:14:16 2008

fhand = open('mbox-short.txt')
for line in fhand:
		line = line.rstrip()
		if not line.startswitch('From ') : continue
		words = line.split()
		print(words[2])

The Double Split Pattern

  • Sometimes we split a line one way, and then grab one of the pieces of the line and split that piece again

From stephen.marquard@uct.ac.za Sat Jan 5:09:14:16 2008

words = line.split()
email = words[1]
pieces = email.split('@')
print(pieces[1]) 

>>> stephen.marquard@uct.ac.za
>>> ['stephen.marquard', 'uct.ac.za']
>>> 'uct.ac.za'

List Summary

  • Concept of a collection
  • Lists and definite loops
  • Indexing and lookup
  • List mutability
  • Functions: len, min, max, sum
  • Slicing lists
  • List methods: append, remove
  • Sorting lists
  • Splitting strings into lists of words
  • Using split to parse strings
profile
목표 : 부지런한 개발자

0개의 댓글