Python零基础入门-1 从一行代码开始运行Python程序(续)

网友投稿 899 2022-11-19

Python零基础入门-1 从一行代码开始运行Python程序(续)

Python零基础入门-1 从一行代码开始运行Python程序(续)

上篇介绍到14行的文档测试,本文接着将剩余代码看完。(下面代码有些经过更改,不要太纠结行数)

15行:迭代工具(itertools)

代码:

from itertools import groupbylines = '''This is thefirst paragraph.This is the second.'''.splitlines()# Use itertools.groupby and bool to return groups of# consecutive lines that either have content or don't.for has_chars, frags in groupby(lines, bool): if has_chars: print (' '.join(frags))# PRINTS:# This is the first paragraph.# This is the second.

运行结果:

This is the first paragraph.This is the second.

16行:读写cvs文件。(现在是25行代码了,也许之前版本是16行)

代码:

import csv# need to define cmp function in Python 3def cmp(a, b): return (a > b) - (a < b)# write stocks data as comma-separated valueswith open('stocks.csv', 'w', newline='') as stocksFileW: writer = csv.writer(stocksFileW) writer.writerows([ ['GOOG', 'Google, Inc.', 505.24, 0.47, 0.09], ['YHOO', 'Yahoo! Inc.', 27.38, 0.33, 1.22], ['CNET', 'CNET Networks, Inc.', 8.62, -0.13, -1.4901] ])# read stocks data, print status messageswith open('stocks.csv', 'r') as stocksFile: stocks = csv.reader(stocksFile) status_labels = {-1: 'down', 0: 'unchanged', 1: 'up'} for ticker, name, price, change, pct in stocks: status = status_labels[cmp(float(change), 0.0)] print('%s is %s (%.2f)' % (name, status, float(pct)))

18行:八皇后问题(递归),递归式一种写函数的方法,后面会讲到。

代码:

BOARD_SIZE = 8def under_attack(col, queens): left = right = col for r, c in reversed(queens): left, right = left - 1, right + 1 if c in (left, col, right): return True return Falsedef solve(n): if n == 0: return [[]] smaller_solutions = solve(n - 1) return [solution + [(n, i + 1)] for i in range(BOARD_SIZE) for solution in smaller_solutions if not under_attack(i + 1, solution)]for answer in solve(BOARD_SIZE): print(answer)

20.质数生成器 生成器(generator)是一种惰性的数据产生器,通常每次调用只会生成一个数据。

代码:

import itertoolsdef iter_primes(): # an iterator of all numbers between 2 and +infinity numbers = itertools.count(2) # generate primes forever while True: # get the first number from the iterator (always a prime) prime = next(numbers) yield prime # this code iteratively builds up a chain of # filters...slightly tricky, but ponder it a bit numbers = filter(prime.__rmod__, numbers)for p in iter_primes(): if p > 1000: break print (p)

21行:XML/HTML解析

代码:

dinner_recipe = '''

amtunititem
24slicesbaguette
2+tbspolive oil
1cuptomatoes
1jarpesto
'''# From xml.etree.ElementTree as etreetree = etree.fromstring(dinner_recipe)# For invalid HTML use import ElementSoup, StringIO# tree = ElementSoup.parse(StringIO.StringIO(dinner_recipe))pantry = set(['olive oil', 'pesto'])for ingredient in tree.getiterator('tr'): amt, unit, item = ingredient if item.tag == "td" and item.text not in pantry: print ("%s: %s %s" % (item.text, amt.text, unit.text))

运行后报错,提示属性错误,没有该属性。

这也是运行Python程序时经常发生的,因为库的版本和代码的版本不一致,有些接口发生变化。一般复制错误去搜索都能找打解决方案。

根据找到的方法,将‘getiterator’修改为‘iter’:(在第18行)

for ingredient in tree.iter('tr'):

运行结果:

baguette: 24 slicestomatoes: 1 cup

28行:八皇后问题。(自定义异常)

程序可能产生错误,可以通过定义异常来处理。

通过继承Exception异常类,自定义一个异常类。

代码:

BOARD_SIZE = 8class BailOut(Exception): passdef validate(queens): left = right = col = queens[-1] for r in reversed(queens[:-1]): left, right = left-1, right+1 if r in (left, col, right): raise BailOutdef add_queen(queens): for i in range(BOARD_SIZE): test_queens = queens + [i] try: validate(test_queens) if len(test_queens) == BOARD_SIZE: return test_queens else: return add_queen(test_queens) except BailOut: pass raise BailOutqueens = add_queen([])print (queens)print ("\n".join(". "*q + "Q " + ". "*(BOARD_SIZE-q-1) for q in queens))

运行结果:

33行:猜数游戏。通过random随机生成数字

import randomguesses_made = 0name = input('Hello! What is your name?\n')number = random.randint(1, 20)print ('Well, {0}, I am thinking of a number between 1 and 20.'.format(name))while guesses_made < 6: guess = int(input('Take a guess: ')) guesses_made += 1 if guess < number: print ('Your guess is too low.') if guess > number: print ('Your guess is too high.') if guess == number: breakif guess == number: print ('Good job, {0}! You guessed my number in {1} guesses!'.format(name, guesses_made))else: print ('Nope. The number I was thinking of was {0}'.format(number))

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:8. 字符串转换整数 (atoi)
下一篇:Spring&nbsp;Data&nbsp;JPA&nbsp;设置字段默认值方式
相关文章

 发表评论

暂时没有评论,来抢沙发吧~