with open('test.txt', 'r') as f:
file_contents = f.read()
words = file_contents.split(' ')
word_count = len(words)
print(word_count)
list的enumerate
names = ['Corey','Chris','Dave','Travis']
for i, name in enumerate(names,start=0):
print(i, name)
zip
#TODO: 匹配两个list, zip
names = ['Peter Parker','Clark Kent','Wade Wilson','Bruce Wayne']
heros = ['Spiderman','Superman','Deadpool','Batman']
universes = ['Marvel','DC','Marvel','DC']
for name, hero, universe in zip(names, heros, universes):
print(f'{name} is actually {hero} from {universe}')
for value in zip(names, heros, universes):
print(value)
# TODO: class添加属性
class Person():
pass
person = Person()
first_key = 'first'
first_val = 'Corey'
person_info = {first_key:first_val,'last':'Schafer'}
#person_info = {'first':'Corey','last':'Schafer'}
for key, value in person_info.items():
setattr(person, key, value)
for key in person_info.keys():
print(getattr(person, key))