首页 > 编程语言 >python 列表 tips stackOverflow数据统计

python 列表 tips stackOverflow数据统计

时间:2023-06-05 16:38:09浏览次数:50  
标签:SQL python JavaScript print HTML stackOverflow ---------- tips CSS


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%


标签:SQL,python,JavaScript,print,HTML,stackOverflow,----------,tips,CSS
From: https://blog.51cto.com/u_16151322/6417536

相关文章

  • PyInstaller 完美打包 Python 脚本,输出结构清晰、便于二次编辑的打包程序
    引入问题如果我要写一个Python项目,打包成exe运行(方便在没有Python的电脑上使用),我需要打包出的根目录结构美观,没有多余的、杂乱的依赖文件在那里碍眼,而且需要在发现bug时,我还需要能够修改里面的代码后,无需再次打包,就能正常运行,该怎么做呢?就以一个Hello项目为例,记一下我......
  • Python实验课5
    实验任务六实验源码:1withopen('data6.csv','r',encoding='gbk')asf:2data=f.readlines()3data1=[str(eval(data[i]))foriinrange(1,len(data))]4data2=[str(int(eval(data[i])+0.5))foriinrange(1,len(data))]5info=......
  • python操作Excel,读取表格内容,并根据需求将内容填充到表格指定位置
    在日常工作中经常会遇到需要将“表一,部门信息表”内容逐行填充到“表二,个人基本信息表”的情况。表一:表二:  实现方式如下:#!usr/bin/python#-*-coding:utf-8-*-importpandasaspdimportopenpyxlasopfromopenpyxlimportWorkbookfromopenpyxlimportl......
  • 用 Python3 & OpenCV 将视频转成字符动画
    在介绍如何用Python3&OpenCV将视频转成字符动画之前,先简单的介绍一下OpenCV吧,毕竟可能很多小伙伴不太了解:百度百科:OpenCV是一个基于BSD许可(开源)发行的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上。它轻量级而且高效——由一系列C函数和少量C++类构成,......
  • python爬虫爬取快手视频多线程下载功能【fd的使用】
    环境:python2.7+win10工具:fiddlerpostman安卓模拟器首先,打开fiddler,fiddler作为http/https抓包神器,这里就不多介绍。配置允许https 配置允许远程连接也就是打开http代理 电脑ip:192.168.1.110然后确保手机和电脑是在一个局域网下,可以通信。由于我这边没有安卓......
  • pip 安装错误 Command "python setup.py egg_info" failed with error code
    pythonsetup.pyinstallFile"setup.py",line7defread(rel_path:str)->str:^SyntaxError:invalidsyntax wgethttps://bootstrap.pypa.io/pip/2.7/get-pip.pypythonget-pip.pypipinstall--upgradesetuptools......
  • python xml 响应断言
    XML断言fromrequests_xmlimportXMLSessionsession=XMLSession()r=session.get('https://www.nasa.gov/rss/dyn/lg_image_of_the_day.rss')print(r.xml.links)XPath断言fromrequests_xmlimportXMLSessionsession=XMLSession()r=session.get(�......
  • 【python】打包py文件
    pyinstaller安装方法步骤:①安装打包用的模块pipinstallPyInstaller②创建虚拟环境pipinstallpipenv#用于搭建虚拟环境pipenvinstall#创建一个新的虚拟环境pipenvshell#进入这个虚拟环境pipinstall--#通过pip安装程序所需要的模块PyInstaller-i1.i......
  • 16个好用到爆的Python实用技巧!
    介绍人生苦短,快学Python!Python是一门用途广泛的编程语言,它具有大量的库和框架。有一些鲜为人知的Python编码技巧和库可以让你作为开发人员的工作更为轻松,编写代码更高效。本文将探讨一些鲜为人知的Python技巧,这些技巧非常有用,但并不广为人知。通过学习和使用这些技巧,可以......
  • 学习Python能做什么 怎么快速学习学习Python
    学习Python能做什么?Python是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言,设计具有很强的可读性。相比其他语言经常使用英文关键字,其他语言的一些标点符号,Python具有比其他语言更有特色语法结构。Python被称为“胶水语言”,拥有丰富而强大的库,在数据分析、数据挖......