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

What are the Python skills for tracking data?

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "what are the Python skills of tracking data". In daily operation, I believe that many people have doubts about the Python skills of tracking data. The editor consulted all kinds of information and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts of "what are the Python skills of tracking data?" Next, please follow the editor to study!

1. While executing the Loop command, follow up and assume that there is a list of friends' names. You have to traverse the list and track the count. What should I do? Just use enumerate.

> friends = ['Ben',' Kate', 'Thinh'] > for I, item in enumerate (friends): > print (f' {I}: {item}') 0: Ben 1: Kate 2: Thinh

Or simply use dictionarycomprehension

> {I: friends [I] for i in range (len (friends))} {0: 'Ben', 1:' Kate', 2: 'Thinh'}

two。 Update DictionaryItems function

If you are using the dictionary function to track the words and the number of words in the first sentence.

Sent1 = {'love': 1,' hate': 3

But when it comes to the second sentence, you want to update the previous dictionary function with the new statement.

Sent2 = {'love':2,' flower': 1}

The updated word package is as follows:

{'love':3,' hate': 3, 'flower': 1}

What should I do? Wouldn't it be nice if there were some tools that could help you do it easily? If you just need such tools, collections.Counter is exactly what you want. A class like collections.Counter allows the existence of multiple elements in a collection

Fromcollections import Counter bag_words = Counter () sent1 = {'love': 1,' hate': 3} bag_words.update (sent1) sent2= {'love': 2,' flower': 1} bag_words.update (sent2) bag_words

Results:

Counter ({'love':3,' hate': 3, 'flower': 1})

Great! Now, when you gather more information from other statements, you can easily update the word pack. You can use len to find out how many unique words there are in a sentence.

> len (bag_words) 3

Alternatively, you can use sum to calculate the total number of words in a statement

> > sum (bag_words.values ()) 7

3. Using Namedtuple to define reusable objects

Want to track a list of information about friends and prepare for their birthdays. Because no information is temporarily available, you need to create a placeholder first so that you can enter information in it later. If you want to record Kate's birthday, favorite food, skin color, and introversion, you can do this:

> Kate = Friend ('Feb',' cake', 'pink', True)

In addition, if you can't remember her birthday, you can call

> > Kate.birthday 'Feb'

Class objects in Python can instantiate Kate, but creating a Friend class to hold simple information is time-consuming. In this case, namedtuple is a good choice. Namedtuple allows records to define a reusable object, ensuring that the correct archive name is used

Fromcollections import namedtuplenamedtupleFriend = namedtuple ('Friend',' birthday foodcolor introvert') Kate = Friend ('Feb',' cake', 'pink', True) Ben = Friend (' Jan','fish', 'red', False)

Display information about Kate:

> Kate Friend (birthday='Feb', food='cake', color='pink', introvert=True)

If you want to know whether Ben is introverted or extroverted, you can call

> Ben.introvert False

With nametuples, users can easily reuse the same object to instantiate new information. Read and practice carefully, and you will learn to use enumerate, set comprehension, Counter, and namedtuple to track information.

At this point, the study of "what are the Python skills for tracking data" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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

Development

Wechat

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

12
Report