|
|
今天要介绍的是关于使用简单的方法解决python问题的。作为一名编码人员,从描述数据类型开始学习和解释语言新概念的方法。那么python中的数据类型是什么?数据类型或简单类型是数据的属性,它告诉解释器程序员打算如何使用数据。众所周知,python中最常见的数据类型是float(浮点数),int(整数),str(字符串),bool(布尔值),列表和dict(字典)。但是与C或C ++不同,Python将自动确定将数据存储为哪种数据类型。但是您需要了解数据类型,才能将数据从一种类型转换为另一种类型(也称为Typecasting或Type conversion)。让我们看一个基本数据类型(清单)的示例,以整体上理解语言和编程。
# Q+ w. R! I7 N) q- <p> ## I am simply creating a list and printing out the type of indivisual
$ A0 k, u+ p/ L9 Y+ L) o - elements in the list ..</p>' M- X1 ]) R# y
- <p> List = [1,2.0,"Hello World",True,[1,2,3,4,5],{'key':'value'}]# I am using * n/ O+ e6 n/ j8 P6 }# U8 @$ J
- for in loop because the size of the list is fixed ..for element in List:</p>
+ Y) Q7 C: r- {5 e - <p> print(type(element))# or you can also use list compreshension</p>7 l$ g2 m. ?" ^8 U
- <p> elements = [type(element) for element in List]</p>
: L) i: ]( u& Y( ? - <p> print(elements)</p>
复制代码
- M& o; K) v+ x* Q; l. p$ W #output //
! S' Z# X$ m, E! J- v( h7 l6 b <CLASS'INT'>
" K* W- v- O* T0 ^- S, Z$ I/ h <CLASS'FLOAT'>
1 J1 ?7 H/ d; O. Z <CLASS'STR'>
, x# c$ U9 @4 D <CLASS'BOOL'>
2 E S4 P7 T6 c5 u < class'list'> <7 Q" c3 `5 e( V
class'dict'>+ l& x, C7 }/ ?& v8 ^ V
[<CLASS'INT'>,,<CLASS'STR'>,<CLASS'BOOL'>,<CLASS'LIST'>,<CLASS'DICT'>]]7 o9 {: w, A- Y
记住 —除了python提供的数据结构外,我们可以在python中创建自己的数据结构。您要做的就是创建一个类,并在该类中定义自己的方法。这 就是创建熊猫数据框的方法……但是我有一天会写它。现在,让我们坚持一些基本知识!/ Z; W& O6 k$ b$ w
基本清单方法
' Q" v1 D/ i* \ U* c 让我们看一下一些List方法,用于从列表中插入和删除元素…
/ t- i1 e! h5 v) r" F2 q3 o 1. append:在列表中添加元素。性能:将元素附加到列表末尾的时间复杂度为o(1),而其他任何地方均为o(n)' m3 f2 o. Q2 p; a
2. remove:删除列表中的元素。性能:删除列表中元素的时间复杂度为o(n)
( B* i+ N. h$ d7 j1 X6 S l 搜索列表中元素的时间复杂度始终为o(1)3 B6 ?& U5 r2 m# H; _/ Y* |
- <p> ## List Methods ...</p>
/ e" y( H E& q2 C7 t x - <p> # insertion</p>4 V9 h) P4 R4 {* g
- <p> List.append((1,5.0))</p>- B1 x, g/ ~2 w' _/ I% V
- <p> print(List)</p>
: ]* \& `& T+ V+ q, O; F, K n - <p> # seraching an element in the list which is already in a list ...</p>
0 ?; S% k! c8 c( z! [ - <p> list_in_list = List[4][0]</p>
/ t3 z3 \/ r. D - <p> print(list_in_list)</p>* I, @# x8 w8 M
- <p> # deletion</p>
! @2 {/ l4 u- q* X - <p> List.remove(1)</p>) ?" q. m' @: e; ?8 Z3 ]3 ^
- <p> print(List)</p>
复制代码
9 ?* W& h6 G( V8 {9 F r( O #output //- m# Q0 |0 ^3 A8 W8 P
[1,2.0,'Hello World',True,[1,2,3,4,5],{'key':'value'},(1,5.0)]0 n. M. `8 ~; z$ n6 h
1
' P* F/ X0 S4 J$ @ [2.0,'Hello世界”,真,[1、2、3、4、5],{'键':'值'},(1、5.0)]
) o. R8 G- \; ~1 p 操作清单项目' u1 U3 ]$ d$ f) `$ {
1. lambda函数:它们以1行编写,用于定义自定义函数/ S& j: `$ m+ z$ Z6 M) ~" i6 M
2. map()方法(在DataScience中也称为向量方法):使用map函数将您定义的自定义函数映射到列表的每个元素。map方法采用2个参数—
. N; u0 I6 {8 q \5 ` 一个。您要应用的功能
2 }; Y) g- j8 F5 _ b。列出您想要上述功能的应用, q' `/ a- p$ c, J! z
3. enumerate()方法: enumerate()方法将计数器添加到可迭代对象并返回它。返回的对象是一个枚举对象。
1 }0 a5 M- b6 t+ {0 g# ?- <p> custom_list = [[1,2,3,4,5],["Cat","Dog","Dog","Tiger","Lion","Goat"]]</p>
6 y2 r( k. g! k' I - <p> # custom function to find square of a number</p>' |# K; M: }$ B% s5 ]5 H" {
- <p> def square_of_number(x):</p>
_9 l5 b: I+ H4 i( E - <p> return x*x</p>
# m* q+ d- f! I - <p> # using lambda functions to create a new list from exisitng list but with
. v/ A3 p0 s! E - cube of each items in the list..</p>
: X. e2 J9 n7 y3 |2 u6 P/ } - <p> cube_of_number = lambda x:x*x*x</p>
5 Y% h, O' }, F |- K- C - <p> squares_of_list_items = lambda x:x*x</p>
6 y& i0 P2 h% O; _# t4 \, f7 | - <p> # using map function to apply a function to all elements of the list</p>
6 d2 s' E; T. W8 U8 h - <p> new_list_of_squares = list(map(squares_of_list_items,custom_list[0]))</p>
% k8 R& Z; q$ k. u# X7 b - <p> new_list_of_cubes = list(map(cube_of_number,custom_list[0]))</p>- w/ c' I R$ w) N# J$ ]
- <p> print(new_list_of_squares)</p>% U' X' K9 B7 o+ @7 P7 Z: A% D
- <p> print(new_list_of_cubes)</p> b' v: N+ r8 R7 o
- <p> # Using Enumerate to find the count of Each animal in custum_list</p>
) e; f2 ^, d8 z$ s6 o% f3 s - <p> for i,x in enumerate(custom_list[1]):</p>
$ N K2 F6 O7 |9 `( ~$ U. v - <p> print('iterater is :', i ,'and value is ', x)</p>
复制代码
1 e+ @/ b" N4 x" ]" o+ @) q #output //
- {: S+ H: F3 F8 m* i* C6 m: P [ 1、4、9、16、25
- e9 l0 z0 q0 i7 c ]
. ] A& T, Y/ E [1、8、27、64、125 ] <枚举对象,位于0x7f0cf44e4b40>
, l5 u7 d0 N" s+ e. s, ~4 Q iterater为:0,值为Cat! @ [4 H& f# z$ U( D$ M, ?! v
迭代器:1,值为Dog
0 q# K* c1 O6 J 迭代器:2并且值是Dog. ?% d* i" G6 M `6 D; a
迭代器是:3和值是Tiger( m* H# ^- N1 `* F4 u V6 @# n- ~
迭代器是:4并且值是Lion
4 L4 f4 k3 Y$ G: }$ k$ z 迭代器是:5并且值是山羊2 }+ p) n Z' r s' f3 Z
发电机和装饰器:, P) t7 @% `2 F# H6 |
1. Python生成器是创建迭代器/可迭代对象的简单方法。它可以在函数调用时暂停,也可以在整个程序的生命周期中通过使用next()函数在任何时间恢复。
: r9 o' @ ]1 N* w! P. [0 ^* p 2.装饰器是python中的一流对象。这意味着它们可以在任何函数中通过引用传递。装饰器只是一个奇特的名称,也是将函数作为参数传递给另一个函数的方式。它们也被称为可调用对象,它接受一个函数并返回或改变该函数的行为。
! S4 o a3 C3 S: L- <p> # Generators :</p>! a# d. ?# k" }3 x
- <p> def generator():</p>, j+ |* L* D. e% ~
- <p> for i in range(5):</p>
( p; n9 V$ ~9 |4 v# l) M) N& E - <p> yield i</p>
8 R# f3 u% S0 x6 @4 v' g$ L - <p> print(generator) # output://</p>7 x, Z$ w$ E0 K
- <p> a = generator()</p>
) ~! R$ H: U. @# P" y9 y - <p> print(next(a)) # output:// 0</p> r$ J, }6 K0 V- ]/ t. p' n" G" y
- <p> print(next(a)) # output:// 1</p>
/ s1 y# m3 s! O s+ M - <p> # Decorators</p>
5 ^/ Y9 p5 N$ C- f) ^, ]! ]: M - <p> def function1():</p>0 ^6 ^& l0 U$ n: a$ u4 p
- <p> print('this is function 1')</p>$ h4 n: |6 M; [
- <p> def function2(function_argument):</p>
& ^/ m3 H: L) E3 ?2 H, N4 n# N u - <p> function_argument()</p>
, o) X$ Z; _5 \# @! s' g+ k0 q - <p> print('this is function 2')</p>
6 I" X7 ?/ x3 f/ t/ e - <p> new_object = function2(function1) # output:// this is function 1 this is * ]7 u5 p- x. v" F
- function 2</p>
9 v K/ k. j) C - <p> # using it as a decorator function ...</p>9 y! O; Z2 F# ^5 J% m( F
- <p> @function2</p>
& g/ b( E' d% ]: M5 T; W - <p> def function1():</p>$ I& I i5 j$ \ u& a5 ~
- <p> print('this is function 1') # output:// this is function 1 this is function
1 k0 g7 H3 g! I' S U$ j - 2</p>
复制代码
: {4 n* C' M M) i) v: G * args和** kwargs+ J, c! Y' z8 g1 @% h! R' }
args和** kwargs是最重要的关键字,在我的编码风格和编程实践中,我个人经常使用它们。好吧,想象一下!您正在创建函数,却不知道预期的参数数量。在这种情况下,我们使用* args,如果它是字典,则使用** kwargs。
5 z8 r$ B: R. E- <p> def arr(*args):</p>
7 _/ Q& R: b* J0 e' o* a - <p> for i in args:</p>
1 G ^0 h. l- r/ \$ @8 P' g - <p> print(i)</p>1 I: A) C2 X) Y' L8 p: T4 v
- <p> arr(1,2,3,4) # output: // 1 2 3 4</p>% W+ |+ X1 k6 e0 e4 A7 }
- <p> arr(1,2) # output:// 1 2</p>
复制代码 [' k; L8 B- k4 ^
Python是一种很棒的编程语言。它易于学习且易于实施。它支持许多程序包,因此许多黑客,数据科学家和后端开发人员都使用它来执行日常任务。
% p4 R9 C3 M9 b2 h. X/ d$ A9 @" _. h! w+ s
|
|