https://zhuanlan.zhihu.com/p/68128557
def find_twelve(num_list1, num_list2, num_list3):"""从 3 个数字列表中,寻找是否存在和为 12 的 3 个数"""for num1 in num_list1:for num2 in num_list2:for num3 in num_list3:if num1 + num2 + num3 == 12:return num1, num2, num3from itertools import product
def find_twelve_v2(num_list1, num_list2, num_list3):for num1, num2, num3 in product(num_list1, num_list2, num_list3):if num1 + num2 + num3 == 12:return num1, num2, num3'''
python-guide: Python best practices guidebook, written for humans.
---
Python 2 Death Clock
---
Run any Python Script with an Alexa Voice Command
---
<... ...>&#39;&#39;&#39;def parse_titles(filename):"""从隔行数据文件中读取 reddit 主题名称"""with open(filename, &#39;r&#39;) as fp:for i, line in enumerate(fp):if i % 2 &#61;&#61; 0:yield line.strip()from itertools import islicedef parse_titles_v2(filename):with open(filename, &#39;r&#39;) as fp:for line in islice(fp, 0, None, 2):yield line.strip()for user in users:if not is_qualified(user):breakfrom itertools import takewhilefor user in takewhile(is_qualified, users):def sum_even_only(numbers):"""对 numbers 里面所有的偶数求和"""result &#61; 0for num in numbers:if num % 2 &#61;&#61; 0:result &#43;&#61; numreturn resultdef even_only(numbers):for num in numbers:if num % 2 &#61;&#61; 0:yield numdef sum_even_only_v2(numbers):"""对 numbers 里面所有的偶数求和"""result &#61; 0for num in even_only(numbers):result &#43;&#61; numreturn resultimport time
import datetimedef award_active_users_in_last_30days():"""获取所有在过去 30 天周末晚上 8 点到 10 点登录过的用户&#xff0c;为其发送奖励积分"""days &#61; 30for days_delta in range(days):dt &#61; datetime.date.today() - datetime.timedelta(days&#61;days_delta)if dt.weekday() not in (5, 6):continuetime_start &#61; datetime.datetime(dt.year, dt.month, dt.day, 20, 0)time_end &#61; datetime.datetime(dt.year, dt.month, dt.day, 23, 0)ts_start &#61; time.mktime(time_start.timetuple())ts_end &#61; time.mktime(time_end.timetuple())for record in LoginRecord.filter_by_range(ts_start, ts_end):send_awarding_points(record.user_id, 1000) def gen_weekend_ts_ranges(days_ago, hour_start, hour_end):"""生成过去一段时间内周六日特定时间段范围&#xff0c;并以 UNIX 时间戳返回"""for days_delta in range(days_ago):dt &#61; datetime.date.today() - datetime.timedelta(days&#61;days_delta)if dt.weekday() not in (5, 6):continuetime_start &#61; datetime.datetime(dt.year, dt.month, dt.day, hour_start, 0)time_end &#61; datetime.datetime(dt.year, dt.month, dt.day, hour_end, 0)ts_start &#61; time.mktime(time_start.timetuple())ts_end &#61; time.mktime(time_end.timetuple())yield ts_start, ts_enddef award_active_users_in_last_30days_v2():"""发送奖励积分"""for ts_start, ts_end in gen_weekend_ts_ranges(30, hour_start&#61;20, hour_end&#61;23):for record in LoginRecord.filter_by_range(ts_start, ts_end):send_awarding_points(record.user_id, 1000)def notify_nonsleep_users_in_last_30days():"""发送通知"""for ts_start, ts_end in gen_weekend_ts_range(30, hour_start&#61;3, hour_end&#61;6):for record in LoginRecord.filter_by_range(ts_start, ts_end):notify_user(record.user_id, &#39;You should sleep more&#39;)