|
|
今天要介绍的是关于使用简单的方法解决python问题的。作为一名编码人员,从描述数据类型开始学习和解释语言新概念的方法。那么python中的数据类型是什么?数据类型或简单类型是数据的属性,它告诉解释器程序员打算如何使用数据。众所周知,python中最常见的数据类型是float(浮点数),int(整数),str(字符串),bool(布尔值),列表和dict(字典)。但是与C或C ++不同,Python将自动确定将数据存储为哪种数据类型。但是您需要了解数据类型,才能将数据从一种类型转换为另一种类型(也称为Typecasting或Type conversion)。让我们看一个基本数据类型(清单)的示例,以整体上理解语言和编程。& H _: V5 r2 f- [6 L
- <p> ## I am simply creating a list and printing out the type of indivisual ) T s, r/ f# e2 `$ p' P
- elements in the list ..</p>/ Q& B/ ] h! t
- <p> List = [1,2.0,"Hello World",True,[1,2,3,4,5],{'key':'value'}]# I am using $ m! h' d- i* f- C* ?% @& U+ i
- for in loop because the size of the list is fixed ..for element in List:</p>
1 G$ q" R5 B+ c7 C" k8 n2 I) c - <p> print(type(element))# or you can also use list compreshension</p>5 m5 B: Y; d/ ~' |
- <p> elements = [type(element) for element in List]</p>7 J) M: j% ^! V* D$ D
- <p> print(elements)</p>
复制代码 & `* d/ t1 |" A
#output //
" `# X# G$ _$ O <CLASS'INT'>
% V) s0 L, l& @3 K <CLASS'FLOAT'>2 G: D' ^! d' _' |% H
<CLASS'STR'>
; Q5 C8 X% m T A0 Z y( I4 v <CLASS'BOOL'>3 i0 ]5 h: t% H+ o: H* Q5 I5 \+ K u
< class'list'> <6 ^$ Q' ]7 {5 @0 \9 b
class'dict'># x. _0 u" u" g+ Q3 o* P9 r1 ?
[<CLASS'INT'>,,<CLASS'STR'>,<CLASS'BOOL'>,<CLASS'LIST'>,<CLASS'DICT'>]]
9 O) P. B' R4 T+ ]3 R, o6 i 记住 —除了python提供的数据结构外,我们可以在python中创建自己的数据结构。您要做的就是创建一个类,并在该类中定义自己的方法。这 就是创建熊猫数据框的方法……但是我有一天会写它。现在,让我们坚持一些基本知识!
2 l) ?( k: l6 s( d 基本清单方法. n7 ?3 l) h5 v- z
让我们看一下一些List方法,用于从列表中插入和删除元素…
5 ]2 D! y7 e3 b e/ a 1. append:在列表中添加元素。性能:将元素附加到列表末尾的时间复杂度为o(1),而其他任何地方均为o(n)
* ]" c8 N' `9 a b9 |& G0 |1 f 2. remove:删除列表中的元素。性能:删除列表中元素的时间复杂度为o(n)
T( ^7 p s# ~$ h; O ] 搜索列表中元素的时间复杂度始终为o(1)
D6 ~- m f$ O _- <p> ## List Methods ...</p>
. ]5 N, z3 P8 N4 R/ Q - <p> # insertion</p>
- G, u4 D t* x% d4 g& D - <p> List.append((1,5.0))</p>, U C7 U/ ~+ H- h" y8 r' K
- <p> print(List)</p>
- m" f* O1 d5 y0 R5 }7 o# s, v - <p> # seraching an element in the list which is already in a list ...</p>! _0 b6 ^9 M4 C
- <p> list_in_list = List[4][0]</p>& J, A% Z" F) c
- <p> print(list_in_list)</p>
& @( P W7 x' Y+ _ - <p> # deletion</p>
8 T9 f! ] ^7 `7 _9 h - <p> List.remove(1)</p>9 ^& _" E9 s6 R+ m) A
- <p> print(List)</p>
复制代码 2 Z7 ^7 U+ Y5 p/ @, m
#output //9 k- D: k) T. l" J# ]1 Y
[1,2.0,'Hello World',True,[1,2,3,4,5],{'key':'value'},(1,5.0)]: f) \9 E2 _ b- z( t
1
7 c7 t4 [9 k# l7 n: ^$ M [2.0,'Hello世界”,真,[1、2、3、4、5],{'键':'值'},(1、5.0)]
, k* z( @6 S1 i! X4 f8 V 操作清单项目
2 q! Z/ _- e& S6 M) M$ m5 {; ` 1. lambda函数:它们以1行编写,用于定义自定义函数$ g* A! ^3 B$ M. P
2. map()方法(在DataScience中也称为向量方法):使用map函数将您定义的自定义函数映射到列表的每个元素。map方法采用2个参数—
( `4 P4 ~# h* w' {7 k 一个。您要应用的功能
; w9 X' ]0 J5 X; `+ [ b。列出您想要上述功能的应用
7 I6 r: D8 V4 H I9 x% } 3. enumerate()方法: enumerate()方法将计数器添加到可迭代对象并返回它。返回的对象是一个枚举对象。
6 N! N: Z% ~* T; r* k- <p> custom_list = [[1,2,3,4,5],["Cat","Dog","Dog","Tiger","Lion","Goat"]]</p>
1 [6 Z; J4 C3 E# C$ B% h! D9 i" y - <p> # custom function to find square of a number</p>, ?+ ~4 {) W& h" P2 s5 B U
- <p> def square_of_number(x):</p>) _/ G2 K# b- r
- <p> return x*x</p>/ M, a) \0 C" \9 H
- <p> # using lambda functions to create a new list from exisitng list but with
$ v, k# v& c4 s0 m5 s4 y7 y - cube of each items in the list..</p>3 u: V- H2 r6 Z) x8 M% i
- <p> cube_of_number = lambda x:x*x*x</p>
: H. n/ f6 h1 a( ?- ^ - <p> squares_of_list_items = lambda x:x*x</p>4 Q5 y& i. N. N- j9 H
- <p> # using map function to apply a function to all elements of the list</p>
6 ?6 `! ^$ m3 [3 x- B - <p> new_list_of_squares = list(map(squares_of_list_items,custom_list[0]))</p>- F4 w. F. G6 }# l* N# y. `! Z; Q
- <p> new_list_of_cubes = list(map(cube_of_number,custom_list[0]))</p>9 d4 I' z7 Z9 @
- <p> print(new_list_of_squares)</p>/ Z* W5 N" g/ d% w; r
- <p> print(new_list_of_cubes)</p>
0 w6 T4 P L$ y ~) ?1 u - <p> # Using Enumerate to find the count of Each animal in custum_list</p>
6 I5 h- ~, @. V - <p> for i,x in enumerate(custom_list[1]):</p>
. [) x* x* Y" P3 M - <p> print('iterater is :', i ,'and value is ', x)</p>
复制代码
x* G2 v9 K! C8 M #output //5 \% P1 o) j4 [- B
[ 1、4、9、16、25( {8 h4 }0 s$ P/ F
]% N% H! I8 d+ I
[1、8、27、64、125 ] <枚举对象,位于0x7f0cf44e4b40>, y! c% R+ ~! {* x. L
iterater为:0,值为Cat2 Z" ^4 H' D4 O+ } s
迭代器:1,值为Dog$ N1 l* V* t; ?* {- u Q% G
迭代器:2并且值是Dog( Q3 f$ d' L2 C, z( p
迭代器是:3和值是Tiger) B& \# C- l% W; H" w2 G, i
迭代器是:4并且值是Lion
3 g: F. j! d! ?, x 迭代器是:5并且值是山羊
, s1 t% P3 ?- O5 c! d+ u 发电机和装饰器:
' w1 z* \4 B" l' k 1. Python生成器是创建迭代器/可迭代对象的简单方法。它可以在函数调用时暂停,也可以在整个程序的生命周期中通过使用next()函数在任何时间恢复。
! W* [& X% [7 K! R9 b 2.装饰器是python中的一流对象。这意味着它们可以在任何函数中通过引用传递。装饰器只是一个奇特的名称,也是将函数作为参数传递给另一个函数的方式。它们也被称为可调用对象,它接受一个函数并返回或改变该函数的行为。( F7 M' z1 B6 V5 b' |
- <p> # Generators :</p>
, g) i4 r: \$ J2 R - <p> def generator():</p>7 R6 r, H' o4 X( p
- <p> for i in range(5):</p>1 o6 |# w( n! q0 o6 B" H
- <p> yield i</p>
6 W; u7 O% r) w6 v9 J - <p> print(generator) # output://</p>1 P' _% i+ R/ }; ]7 @" E
- <p> a = generator()</p>. v" ~$ V8 ?' V8 q1 }7 u4 r5 ~: ^8 ^
- <p> print(next(a)) # output:// 0</p>
0 p& Q- \/ b7 k - <p> print(next(a)) # output:// 1</p>2 @: s+ M0 d8 Y* V
- <p> # Decorators</p>% e- q4 D$ Z6 P u6 W
- <p> def function1():</p>
# i O+ J( _- o1 l/ N8 y; I - <p> print('this is function 1')</p>+ a Z0 |5 A" l! \; G1 M" j! Y
- <p> def function2(function_argument):</p>
- {' m Z( y! r; Z/ b - <p> function_argument()</p>
1 g6 w& f3 D0 X3 Q8 B" H - <p> print('this is function 2')</p>
9 w5 {5 s- M& h/ g! a - <p> new_object = function2(function1) # output:// this is function 1 this is " x* N. A- B) H3 Q1 y( k5 Q
- function 2</p>; B) L @' V; _2 L% c& u% i+ d
- <p> # using it as a decorator function ...</p>) n. S& w! u& U! _" C2 k0 j' }3 c
- <p> @function2</p>, z/ z: w" P8 ^
- <p> def function1():</p>
6 t0 l/ I: }) c- x, B9 u" i - <p> print('this is function 1') # output:// this is function 1 this is function
$ q: w, T+ t# D6 c+ i - 2</p>
复制代码
. q( A) J6 J9 }8 | * args和** kwargs A/ }# i7 K6 W4 x
args和** kwargs是最重要的关键字,在我的编码风格和编程实践中,我个人经常使用它们。好吧,想象一下!您正在创建函数,却不知道预期的参数数量。在这种情况下,我们使用* args,如果它是字典,则使用** kwargs。
$ ~5 L. T9 k+ j3 l- <p> def arr(*args):</p>
% l c# T: N7 T' h/ r; i8 | - <p> for i in args:</p>% i0 T) Z' f; i+ @! Y3 O4 C, }
- <p> print(i)</p>
* S* N$ u& U/ ?- f! ]/ w! }, e8 p/ j - <p> arr(1,2,3,4) # output: // 1 2 3 4</p>
4 j: O; E P! i O5 l5 M - <p> arr(1,2) # output:// 1 2</p>
复制代码
8 J0 D" h3 N1 s Python是一种很棒的编程语言。它易于学习且易于实施。它支持许多程序包,因此许多黑客,数据科学家和后端开发人员都使用它来执行日常任务。1 ]) P5 j6 M$ b0 L) }. t6 Q
7 {* H0 p. g3 R9 V& k5 z: M- M |
|