PYTHON笔记一
Note one**
python中的集合类,有列表,元组,字典和集合四种。定义方式分别为:
List = []
Turple = ()
Dict = {key: value}
Set = {}
作为JAVA程序员看来,python中的集合与JAVA中的集合类其实很相似,用着肯定是python更好用,个人认为优点如下:1.没有强定义类型。2.提供了很方便的处理统计运算方式。3.可能是语言的优点,少量代码就能实现复杂逻辑。
Note two
https://docs.python.org/zh-cn/3.7/library/os.html 作为脚本语言,os模块在脚本中时有用到,os模块能完成查看当前操作系统,执行系统命令,文件管理之类功能。使用方法也很简单。如图所示就能进行使用,具体有哪些方法参见上述文档。
上图中使用到了os.getcwd()查看当前路径的方法。
os.getlogin()查看当前用户方法。
os.getpid()查看当前进程号方法。
Note three
这点感觉超级nice,让数字运算,数字展示更加的简洁,直观。通过数字之间用字符隔开的形式就能做到。代码如下:
# how to make a number more clearly
num1 = 10000000000
num2 = 1000000
total = num1 +num2
# print(total)
var1 = 10_000_000_000
var2 = 1_000_000
# print(f'{total:,}')
Note four
告别四句式if else吧:通过语句
expression define if condition else define远离了四行的if else,实例如下:
condition = True
if condition:
x = 1
else:
x = 0
x = 1 if condition else 0
Note five:
能用contextManger的时候就不要用open的方式,contextManger会在资源不再使用的时候替我们关闭所有资源,代码实列如下:
# f = open('test.txt', 'r')
# file_contents = f.read()
# f.close()
#
# words = file_contents.split(' ')
# word_count = len(words)
# print(word_count)
# use a context manager
with open('test.txt', 'r') as f:
file_content = f.read()
words = file_content.split(' ')
word_count = len(words)
# print(word_count)
代码中未注释的是使用contextManger的用法,唯一好处在于自动完成资源的关闭。毕竟使用python简洁是一种追求。我在写python代码的时候其实python代码的可读性个人感觉弱于java。
Note six
还需要外部定义遍历中的次数吗???代码实列如下;
list = ['liubh1', 'liubh2', 'liubh3', 'liubh4']
index = 0
for name in list:
print(index, name)
index += 1
for index, name in enumerate(list, start=1):
print(index, name)
试试使用enumerate吧,start表示遍历开始从多少开始计数。通过这中方式就可以不需要再外部定义index = 0。极致代码简洁度。Of course,在外部定义遍历索引也是一种很好的方式。
Note seven
列表如何实现一一对应,这个我大学时候写估计还在用index每个list都去获取相应值,然后排在一起,其实有更简洁的方法,不能说它更好,但更简洁,逼格以下就上升了,代码示例如下:
names = ['马云', '王健林', '麻花疼', '雷军']
companies = ['阿里', '万达', '腾讯', '小米']
cities = ['杭州', '大连', '深圳', '武汉']
for index, name in enumerate(names):
company = companies[index]
print(f'{name} belongs to {company}')
for name, company, city in zip(names, companies, cities):
print(f'{name} belongs to {company} from {city}')
还是通过使用enumerate方法,当集合更多的时候可以使用zip()方法,想传多少集合都可以。
Note eight
直接看代码示例:
# a, b = (1, 2)
# print(a)
a, *b, c = (1, 2, 6, 9)
# print(b)
我记得在上大学时经常用到过这样的思路,某个列表或元组要取中间部分,可能第一时间想到的把开头或者尾部取掉,其实通过*号在变量定义的时候就能完成这种操作了。
Note nine
在做java程序员后感觉自己才开始慢慢的懂了,理解了面向对象的思维,python中也有面向对象的概念。通过setattr()和getattr()就能完成对象属性的操作了,这个真的超级棒。
代码实列如下:
class Person():
pass
person = Person()
key = 'age'
value = '20'
setattr(person, key, value)
first = getattr(person, key)
print(first)
person_info = {'age': "20", "name": "liubh"}
for key, value in person_info.items():
setattr(person, key, value)
for key in person_info.keys():
value = getattr(person, key)
###It’a tutural from Ytu,I don’t know can I put the address here,So you can common it about your email so we can get a communication.
一分钟搞定csv统计。使用了非常强大的Counter()。
import csv
#
#
from collections import defaultdict, Counter
# with open('data/survey_results_public.csv', encoding="utf-8") as f:
# csv_reader = csv.DictReader(f)
# yes_count = 0
# no_count = 0
# counts = {
# 'Yes': 0,
# 'NO': 0
# }
# counts = defaultdict(int)
# counts = Counter()
# for line in csv_reader:
# print(line['Hobbyist'])
# if line['Hobbyist'] == "Yes":
# yes_count += 1
# elif line['Hobbyist'] == "No":
# no_count += 1
# counts[line['Hobbyist']] += 1
# total = yes_count + no_count
# yes_pct = (yes_count / total) * 100
# yes_pct = round(yes_pct, 2)
# no_pct = (no_count / total) * 100
# no_pct = round(no_pct, 2)
# print(f'Yes:{yes_pct}%')
# print(f'No:{no_pct}%')
# print(counts)
# with open('data/survey_results_public.csv', encoding='utf-8') as f:
# reader = csv.DictReader(f)
# language_counter = Counter()
# total = 0
# for data in reader:
# languages = data['LanguageWorkedWith'].split(';')
# language_counter.update(languages)
# # most_common查看排名
# total += 1
# print(language_counter.most_common(5))
# for language, value in language_counter.most_common(5):
# language_pct = (value / total) * 100
# language_pct = round(language_pct, 2)
# print(f'{language}: {language_pct}%')
with open('data/survey_results_public.csv', encoding='utf-8') as f:
reader = csv.DictReader(f)
dev_type_info = {}
total = 0
for data in reader:
dev_types = data['DevType'].split(';')
for dev_type in dev_types:
dev_type_info.setdefault(dev_type, {
'total': 0,
'language_counter': Counter()
})
languages = data['LanguageWorkedWith'].split(';')
dev_type_info[dev_type]['language_counter'].update(languages)
dev_type_info[dev_type]['total'] += 1
for dev_type, info in dev_type_info.items():
print(dev_type)
for language, value in info['language_counter'].most_common(5):
language_pct = (value / info['total']) * 100
language_pct = round(language_pct, 2)
print(f'----------{language}:{language_pct}%')
注释第二部分运行结果如下:
JavaScript: 66.63%
HTML/CSS: 62.4%
SQL: 53.49%
Python: 41.0%
Java: 40.41%
2019排名前五的变成语言分别是JS,HTML/CSS,SQL.Python,JAVA。
附代码,对2019 developer survey。Developer中不同岗位什么最受欢迎。
以上代码运行结果如下,展示的是程序员中的不同工种最常使用的编程语言:
----------HTML/CSS:54.9%
----------Python:51.09%
----------JavaScript:50.58%
----------Java:42.71%
----------C++:35.02%
Developer, desktop or enterprise applications
----------JavaScript:67.84%
----------HTML/CSS:64.55%
----------SQL:63.56%
----------C#:53.69%
----------Java:44.69%
Developer, front-end
----------JavaScript:87.72%
----------HTML/CSS:83.62%
----------SQL:58.65%
----------Java:37.6%
----------PHP:35.94%
Designer
----------HTML/CSS:78.88%
----------JavaScript:78.33%
----------SQL:60.18%
----------PHP:40.23%
----------Java:39.44%
Developer, back-end
----------JavaScript:72.23%
----------HTML/CSS:65.42%
----------SQL:64.01%
----------Java:44.03%
----------Python:40.67%
Developer, full-stack
----------JavaScript:86.15%
----------HTML/CSS:78.94%
----------SQL:65.54%
----------Java:40.74%
----------Bash/Shell/PowerShell:37.91%
Academic researcher
----------Python:61.06%
----------HTML/CSS:55.87%
----------JavaScript:54.25%
----------SQL:47.55%
----------Java:42.26%
Developer, mobile
----------JavaScript:67.72%
----------HTML/CSS:62.46%
----------Java:57.21%
----------SQL:51.27%
----------C#:34.34%
Data or business analyst
----------SQL:73.88%
----------HTML/CSS:62.11%
----------JavaScript:61.33%
----------Python:51.86%
----------Bash/Shell/PowerShell:38.43%
Data scientist or machine learning specialist
----------Python:79.33%
----------SQL:58.44%
----------JavaScript:51.38%
----------HTML/CSS:50.43%
----------Bash/Shell/PowerShell:44.49%
Database administrator
----------SQL:81.7%
----------JavaScript:78.11%
----------HTML/CSS:76.19%
----------Bash/Shell/PowerShell:45.2%
----------PHP:44.16%
Engineer, data
----------SQL:66.75%
----------Python:64.31%
----------JavaScript:60.13%
----------HTML/CSS:56.47%
----------Bash/Shell/PowerShell:48.55%
Engineer, site reliability
----------JavaScript:69.43%
----------Bash/Shell/PowerShell:64.05%
----------HTML/CSS:62.79%
----------SQL:61.37%
----------Python:59.23%
Developer, QA or test
----------JavaScript:73.38%
----------HTML/CSS:70.31%
----------SQL:64.81%
----------Bash/Shell/PowerShell:45.73%
----------Java:45.23%
DevOps specialist
----------JavaScript:73.67%
----------HTML/CSS:66.66%
----------SQL:64.56%
----------Bash/Shell/PowerShell:63.98%
----------Python:52.44%
Developer, game or graphics
----------JavaScript:69.02%
----------HTML/CSS:66.37%
----------C#:54.31%
----------SQL:48.91%
----------C++:47.85%
Educator
----------JavaScript:70.15%
----------HTML/CSS:70.15%
----------SQL:56.92%
----------Python:47.02%
----------Java:44.26%
Student
----------HTML/CSS:68.13%
----------JavaScript:63.53%
----------Java:54.37%
----------Python:54.37%
----------SQL:51.83%
Engineering manager
----------JavaScript:72.35%
----------HTML/CSS:65.02%
----------SQL:60.4%
----------Bash/Shell/PowerShell:49.1%
----------Python:46.86%
Senior executive/VP
----------JavaScript:75.94%
----------HTML/CSS:71.81%
----------SQL:64.12%
----------Bash/Shell/PowerShell:46.8%
----------Python:46.37%
System administrator
----------JavaScript:73.45%
----------HTML/CSS:72.57%
----------SQL:67.94%
----------Bash/Shell/PowerShell:58.44%
----------Python:51.36%
Developer, embedded applications or devices
----------JavaScript:60.89%
----------HTML/CSS:57.75%
----------C++:51.08%
----------SQL:50.97%
----------Python:50.95%
Product manager
----------JavaScript:75.0%
----------HTML/CSS:71.92%
----------SQL:63.42%
----------Python:39.63%
----------Bash/Shell/PowerShell:38.96%
Scientist
----------Python:69.48%
----------HTML/CSS:51.04%
----------JavaScript:48.77%
----------Bash/Shell/PowerShell:47.83%
----------SQL:44.21%
Marketing or sales professional
----------HTML/CSS:76.82%
----------JavaScript:71.79%
----------SQL:58.97%
----------PHP:44.21%
----------Python:38.26%