Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Ways to improve the efficiency of python Learning

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains the "ways to improve the learning efficiency of python", the content of the explanation is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "ways to improve the efficiency of python learning"!

We usually run the data when we may store the data results in the txt file, do not know how you usually deal with the data in the txt file, I believe that students have their own methods, using python pandas package or stuffing the data into the database and then using sql and so on. No matter which method is used when dealing with data, there are many common methods, such as where,join, etc., you can first write these common methods into python scripts, and use scripts to deal with txt files directly when you need to deal with the data of txt files. The advantage lies in that it saves the time of tossing data back and forth between txt and database, as well as the time of reading data and writing scripts with pandas, and can quickly and easily verify and process data.

Before giving an example, we should first introduce a concept of "pipe" in linux. Those who are familiar with linux should be familiar with this concept, with the symbol "|". The function of a pipe is to connect multiple commands such as the command: cat data.txt | wc-l means to view the number of pieces of data in data, where "|" is a pipe, and the output of cat data.txt is used as the input of wc-l. To sum up, as long as the first command writes to standard output and the second command reads from standard input, the two commands can form a pipeline. Similarly, we can use to pass the output to the python script.

Now that we understand the concept of pipeline, let's get started. The case data are as follows:

Data1.txt records the user's id and age, and data2.txt records the user's consumption information

First, let's write a python script that implements the where function. The script is as follows:

Where.py

#! / usr/bin/env python #-*-encoding:utf-8-*-import sys import re import cutmode def where (col, cmpexpr, val, cmptype): sw = {'>': lambda y, x: y > x,'> =': lambda y, x: y > = x,'='25 int

Join.py

#! / usr/bin/env python #-*-encoding:utf-8-*-import sys import re def makeJoin (joinfields, file_list= []): dict = {} file_last = open (file_list [- 1]) k, v = joinfields [- 1] .split (':') k, v = int (k), int (v) for line in file_last: sps = re.split ('\ sfolk, line) if len (sps) > = max (k) V): val = sps [v] if v > = 0 else''dict.setdefault (sps [k], val) file_last.close () for i in xrange (len (file_list)-1): fd = open (file_ list [I] 'r') field = joinfields [I] .split (':') [0] for data in fd.readlines (): attr = re.split ('\ tcut, data.strip ()) if len (attr) = max (col1,col2)): if col1 > = 0 and col2 > = 0 and col1

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report