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 skills of Python statistical methods

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

Share

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

This article introduces the relevant knowledge of "what are the methods and skills of Python statistics". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

First, use the dictionary dict statistics

Loop through the element of an iterable object, and if there is no element in the dictionary, let the element be the key of the dictionary, assign the key to 1, and add 1 to the corresponding value of the element if it exists.

Lists = [count_dist = dict () for i in lists: if i in count_dist: count_ [I] + = 1 else: count_ [I] = 1print (count_dist) # {'await: 2, 1: 2, 2: 1, 3: 1} 2, using collections.defaultdict statistics

Defaultdict (parameter) accepts a type parameter, such as int, float, str, and so on.

The type parameters passed in are not used to constrain the type of value, let alone the type of constraint key, but to initialize a value when the key does not exist.

Defaultdict (int)-- initialized to 0defaultdict (float)-- initialized to 0.0defaultdict (str)-- initialized to''from collections import defaultdictlists = [' axiaqiang int for i in lists: count_ count_ [I] + = 1print (count_dict) # defaultdict (, {'aqu: 2,' baked: 1, 1: 2, 2: 1, 3: 1}) III.

The count () method is used to count the number of times an element appears in the list.

Use syntax:

# use syntax list.count (obj) # number of returns

Count the number of times a single object:

# count the number of times per object aList = [123,' abc', 'good',' abc', 123] print ("Count for 123:", aList.count (123)) print ("Count for abc:", aList.count ('abc')) # Count for 123: "Count for abc: 2

Count the number of times for each object in List:

Test = ["aaa", "bbb", "aaa", "aaa", "ccc", "ccc", "ddd", "aaa", "ddd", "eee" "ddd"] print (test.count ("aaa")) # 4print (test.count ("bbb")) # 1test_result = [] for i in test: if i not in test_result: test_result.append (I) print (test_result) for i in test_result: print (f "{I}: {test.count (I)}")'41 ['aaa',' bbb', 'ccc',' ddd' 'eee'] aaa:4bbb:1ccc:2ddd:3eee:1''' IV. Use set and list statistics

First use set to remove weight, and then loop each element and the corresponding number of times list.count (item) into tuples.

A: there are no answers to problems encountered in study. The editor has created a Python learning exchange group: 531509025 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Lists = [count_set = set (lists) print (count_set) # assemble to get rid of # {1meme 2pje 3, 'baked,' a'} count_list = list () for i in count_set: count_list.append ((I, lists.count (I)) print (count_list) # [(1pc2), (2,1), (3pr 1), ('baked 1)] ('asides, 2)] 5. Collections.Counter method

Counter is a container object, and the statistics of hash objects can be realized by using the Counter class in the collections module.

Counter is an unordered container type that is stored as a dictionary key-value pair, where the element is key and its count is value.

The count value can be any Interger (including 0 and negative numbers).

The Counter () object also has several callable methods:

Most_common (n)-- TOP n elements with the highest frequency

Elements-get all the keys converted through list

Update-add object

Subtrct-Delete object

Subscript access a ['xx']-returns 0 if it does not exist

Import collectionsc = collections.Counter ('helloworld')

Directly display the frequency of each element

Print (c) # Counter ({'lump: 3,' oasis: 2, 'hype: 1,' eBay: 1, 'wicked: 1,' ritual: 1, 'dudes: 1})

Use most_common to display the most n elements

When multiple elements have the same count value, the arrangement is in no definite order.

Print (c.most_common (3)) # [('lags, 3), (' oaks, 2), ('hacks, 1)]

Get it using an array subscript, similar to a dictionary:

Print ("The number of'okeeper:", c ['o']) # The number of'okeeper: 2

Statistical list: (as long as the objects in the list can be hashed)

Import collectionsx = [1dictc 2, 2: 1, 3: 2, 4: 2, 5: 2, 6: 1, 7: 1] print (c.most_common (3)) # [(8, 2), (1, 2)] dictc = dict (c) # convert to dictionary print (dictc) # {1: 2, 2: 1 3: 2, 4: 2, 5: 2, 6: 1, 7: 1, 8: 4}

If there are unhashalbe objects in the list, for example, a mutable list, it cannot be counted.

Tuples can also be counted.

C = collections.Counter ([[1Mague 2], "hello", 123,0.52]) # TypeError: unhashable type: 'list'

After you get the Counter counter object, you can also make incremental updates based on it.

Elements ()-returns the iterator

Elements are arranged in no definite order, and elements with a number less than 1 are not included.

A: there are no answers to problems encountered in study. The editor has created a Python learning exchange group: 531509025 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Import collectionsc = collections.Counter (Apocalypse 4, Bethle4, Benz2, print, 1) print (c) # Counter ({'axie: 4,' baked: 2, 'cedar: 1}) list (c.elements ()) # [' await, 'baked,' axed, 'baked,' c']

Subtract function-- subtract elements

Import collectionsc = collections.Counter (["a", "b", "c", "a"]) print (c) # Counter ({'aqu: 2,' baked: 1, 'croup: 1}) print (list (c.elements () # expand # [' a','a', 'baked,' c'] # reduce element c.subtract (["a", "b"]) print (c) # Counter ({'aqu: 1) 'cased: 1,' baked: 0}) print (list (c.elements () # ['averse,' c']

Update function-- add elements

The update function is very useful when doing incremental counting.

A: there are no answers to problems encountered in study. The editor has created a Python learning exchange group: 531509025 look for like-minded partners to help each other, and there are also good video learning tutorials and PDF e-books in the group! Import collectionsc = collections.Counter (["a", "b", "c", "a"]) print (c) # Counter ({'aqu: 2,' baked: 1, 'croup: 1}) print (list (c.elements () # expand # [' a', 'baked,' baked,'c'] c.update (["a", "d"]) print (c) # Counter ({'averse: 3,' baked: 1) ) print (list (c.elements () # ['await,' averse, 'baked,' clocked,'d']

Del function-delete key

When the count value is 0, it does not mean that the element is deleted, and the deleted element should use del.

Import collectionsc = collections.Counter ('helloworld') print (c) # Counter ({' lump: 3, 'oval: 2,' hype: 1, 'eBay: 1,' wicked: 1, 'ritual: 1}) c ["d"] = 0print (c) # Counter ({' lump: 3, 'oval: 2,' hinge: 1, 'ethereal: 1,' wicked: 1, 'ritual: 1 (del: 0}) del c ["l"] print (c) # Counter ({'oclinic: 2,' hype: 1, 'eBay: 1,' wicked: 1, 'ringing: 1,' dice: 0}) this is the end of the introduction to "what are the skills of Python statistical methods" Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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: 300

*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