Get the App
SLTechnology News&Howtos  ›  Development  › 

How to solve the problem of Python deduction, generator and slicing

Shulou Source: shulou.com Published: 2022-06-01 02:24:06 10月09日 Update

This article introduces the relevant knowledge of "how to solve the problem of Python deduction, generator and slicing". 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!

I. Experimental requirements

1. Understand and master the common operations in the sequence.

two。 Understand and master the usage of deduction and slicing and be able to solve practical problems.

2. Content of the experiment

1. Write a program to test the frequency of characters.

# use dict method1data = frequences=dict () for item in data: if item in frequences: frequences [item] + = 1 else: frequences [item] = 1print frequences#use dict method2frequences = dict () for item in data: frequences [item] = frequences.get (item 0) + 1print frequences#use defaultdictfrom collections import defaultdictfrequences = defaultdict (int) for item in data: frequences [item] + = 1print frequences.items () # use set and list typecount_set = set (data) count_list = [] for item in count_set: count_list.append ((item,data.count (item)) print count_list#use collections.Counterfrom collections import Counterfrequences = Counter (data) print frequences.items () print list (frequences.elements ()) # list all the elementsprint frequences.most_common (3)

2, write a program to find the sum of all odd numbers within 100.

Sum = 0for i in range (1m 100jue 2): sum = sum + I print (sum)

3, write a program to generate a list of 30 random integers, and then arrange the elements of the even subscript in descending order, while the elements of the odd subscript remain the same.

Import randomx = [random.randint (0100) for i in range (30)] # print (x) print x look at the original list y = x [:: 2] # print (y) print even coordinates y.sort (reverse=True) x [:: 2] = yprint (x) "how to solve the Python derivation, generator and slicing problem". Thank you for 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!

Tags: Problem generation content program generator subscript even element odd actual more knowledge experiment practicality achievement next dilemma coordinates characters commonly used Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Redmi Microsoft NVidia MariaDB