sardine2 / python

Life is short, I need python.
0 stars 0 forks source link

ch1 task #10

Open sardine2 opened 7 years ago

sardine2 commented 7 years ago

读了一些上传作业的文章,总结如下:

首先从结构和对目标处理来看,同学大致分两类,一是使用函数,另一种是不用。

使用函数 https://github.com/yanzhiw/Py103/blob/master/Chap1/project/weather_check.py https://github.com/Davidfupenghao/Py103/blob/master/Chap1/project/weatherSearch.py https://github.com/goldandelion/Py103/blob/master/Chap1/project/weather.py https://github.com/Ricardotang/Py103/blob/master/Chap1/project/weather_info_beta2.py https://github.com/VyGiBe/Py103/blob/master/Chap1/project/1w_task.py https://github.com/JonasCynical/Py103/blob/master/Chap1/project/weather.py 上面的代码有的使用函数作为一个工具处理某部分对象

这几个比较典型

https://github.com/gogu/Py103/blob/master/Chap1/project/main.py https://github.com/Leohb/Py103/blob/master/Chap1/project/seach1.py https://github.com/Yifan127/Py103/blob/master/Chap1/project/weather_report.py 未使用函数 https://github.com/shippomiru/Py103/blob/master/Chap1/project/ch1.py https://github.com/leiyunhe/Py103/blob/master/Chap1/project/1w-query.py https://github.com/betterMax/Py103/blob/master/Chap1/project/weather_v4_history.py https://github.com/chuanwj/Py103/blob/master/Chap1/project/weather.py https://github.com/draachen/Py103/blob/master/Chap1/project/weather_query.py https://github.com/Yzlovekid/Py103/blob/master/Chap1/project/demo_weather_v1.py https://github.com/Liumang/Py103/blob/master/Chap1/project/weather_search.py https://github.com/CallingCrab/Py103/blob/master/Chap1/project/weather_search.py https://github.com/shereb/Py103/blob/master/Chap1/project/ch1.py https://github.com/leilayanhui/Py103/blob/master/Chap1/project/chap1.weather.v4.py https://github.com/guoyuming/Py103/blob/master/Chap1/project/welcome_module.py

sardine2 commented 7 years ago

gzMichael 和 yanzhiw 同学都 import 了 codecs 模块,采用了f = codecs.open()方式来打开,相比直接用 open() 的好处是?

sardine2 commented 7 years ago

str.format(*args, **kwargs)

YIFAN同学用了这个。

sardine2 commented 7 years ago
def weather_dict01():
    with open('weather_info.txt', 'rt') as f:
        for line in f:
            str = line.split(',', 1)  
            data[str[0]] = str[1]
    return data
def weather_dict01():
    f = open('weather_info.txt','r').readlines()
    for line in f:
        data1 = line.split(',')
        d[data1[0]] = data1[1].strip()
with open("weather_info.txt","r", encoding = "utf-8") as f:
    city_weather_dict = {}
    for line in f:
         s = line.split(',')  #split: turning a string with a certain separator into a list.
         city_weather_dict.setdefault(s[0], s[1])

Both is ok

sardine2 commented 7 years ago

str.capitalize() Return a copy of the string with its first character capitalized and the rest lowercased.

sardine2 commented 7 years ago

还有待学习的:

  1. import time
  2. 把用户输入的中文拼音转换成 中文,然后去查询
  3. 查询历史的 print 格式
sardine2 commented 7 years ago

yanzhiwu同学:

@guoyuming 刚看了你的代码,结构非常舒服,如果说你的构造是美国式的,那么我的估计就是苏联式的了能用就好不考虑结构。

给一个小建议

  d = {}
        with open("weather_test.txt") as f:
            for line in f:
                key, value = line.strip().split(',')
                d[key] = (value)
        enterin = input("请输入查询城市名称:")

这段代码最好放在while前面,不然你每次输入look之后,程序都需要重新做一次dict,等于重复造轮子了。

sardine2 commented 7 years ago

大神 gogu:

我也思考了这个问题。这样做在程序的功能上是有不同之处的:假设 weather_test.txt 是一个时常更新的文件,则每次重新读取文件重新创建 dict 就显得相对合理些了。 当然更实时的是键入命令后检查当前文件有没有变动,再重读文件 ...

sardine2 commented 7 years ago

guoyuming 同学

 with open("weather_test.txt") as f:
        for line in f:
             key, value = line.strip().split(',')
             d[key] = (value)

这段代码灵感应该是源自于Python - file to dictionary? - Stck Overflow

sardine2 commented 7 years ago

总结: 至于用函数和不同函数,还是偏向于用函数。 先用函数实现几个不同的功能,再在主干的if...elif...else分支中使用各个函数。

  1. 查看每个功能都非常的简洁, 2.主干部分也不会因为每个功能的细节凑在一起而显得十分杂乱。
sardine2 commented 7 years ago

关于 查询历史的输出方式:

  1. 大猫,在上一步将数据添加到列表里时,就添加了tuple((city,weather))这样一个二元数据,然后在打印出来时,则用

    for history in list:
    print(history[0],history[1])

    这里的history其实指的就是所有(city,weather),history[0]指的是city,history[1]指的是weather

  2. 我用的是 dict。主要是我当时只懂这一个方式。 用户查询历史时只添加city这一个数据到列表里,然后在输出时直接使用字典将weather输出。

sardine2 commented 7 years ago

from leohb 同学:

city_dict = dict(item.split(",") for item in weather_fo) 上面是 遍历weather_fo每一行 -> item,对字符串根据“,”来分离,初始化city_dict 有个疑问,按我的理解,item.split(",") 这也是列表,将字符串分列后的list 为啥上面可以直接将列表转换成字典呢?


faketooth: 我来催个债: 个人笔记的2.2/2.3章节提到的疑问,尚未发出issue,也没有提供结论


我理解是 item.split(",") 是列表,每个列表只有一个元素,一个为地名,一个是天气情况。 其中一个列表作为 dict 的 key ,一个作为 dict 的value,类似:{ [北京]:[晴]} 。

sardine2 commented 7 years ago

关于 encode and decode

sardine2 commented 7 years ago

有待完成的练习 《python 编程快速上手》3.8 一个小程序:猜数字

sardine2 commented 7 years ago
while(True):

    k = input("城市:")

    # case1:quit .  Similar method:capitalize()  upper()  lower()  
    if k.capitalize() in {"Quit","Exit","E","Q"}:  #convert the first letter to Capitalize, 
        print("您查询过的城市有{}个:".format(len(cities)))
        # The method split is the opposite of join
        # join (joins a list of strings with another string as a separator)
        print("、".join(cities),"\n本次查询结束!")  
        break
    # case2: return the weather of cities
    if k in city_weather_dict:
        if k not in cities:  # remove duplicate elements
            cities.append(k)
        print("天气:" + city_weather_dict[k])  # remove leading characters(spaces)

    # case3: 获取帮助信息
    elif k.capitalize in {"help", "h"}:
        with open("README.md","r", encoding = "utf-8") as f1:
            print(f1.read())

    # case4: cities don't exist
    #elif k not in city_weather_dict:
    else:
        print("对不起,你所输入的城市信息不存在,请尝试查询其他城市!")
sardine2 commented 7 years ago

learning

sardine2 commented 7 years ago

又 update 一次 ch1作业版本 use function as more as possible 根据大猫同学的代码,每天晚上花一小时,修改了ch1代码,加了查询时间,加了历史记录排序,加了退出时先打印历史记录的功能,然后把它们都用函数封装起来。 这样看起来微不足道的 update,我也很高兴了~~ 今天看到一个关于程序员未来职业规划的知乎回答,觉得确实在学习心态上,正式程序员与我这种纯属玩票性质的业余爱好者是完全两个世界啊。