关于列表的一些操作函数

this is a note

l0 = 'acoilia.kinlrmi.3000.clevorihno'
l1 = l0.split('.')  #列表是可变类型
print(l1)			#li现在是列表了

def return_function():           #these functions return values
    a = l1[0]                   #list1 = [index of the element]
    #b = len(l1)                 #shows how many elements in list.
    #c = 'kinlrmi' in l1         #"in" or "not in". return a bool type

    #d = l1.index('clevorihno')  #↓.no such a "find"function in list_operation
    #e = l1.count('acoilia')     #same as the "count"function in str_operation
    #f = l1.copy()               #copy list and reurn value to f

def none_return_function():      #changes original list directly and no return valuesa
    l1[0] = 'ahhh '             #[index],change [index] to "ahhh "
    #l1.reverse()                #as function's name says
    #l1.sort()                   #default:(reverse = false)
    #l1.append('is')             #only add new data/list to end of l1.
    #l1.extend(['my','friend'])  #able to add new list's elements to end of l1.('str')is also right form

    #l1.insert(0,'the')          #(index,'data')
    #l1.remove('the')            #('data')

    l1.clear()                  #after this,list is an empty list.

def speical_function():
    #f = l1.pop()    #(index).default:pop the last one
                     #↑pop is more like "pick it out of list" but "delete",cuz var j returns a data,which is what's deleted.

    #1.sort()        #ording by initial character when dealing with a string contains letter/number.
                     #↑this is only one of many ways to use

    #del l1[0]       #delete [index] element.
    del l1          #delete the whole list,include var_name.

return_function()
none_return_function()
speical_function()
print(l1)

需要验证哪个函数把#取掉就好了。另外这位博主写的文章很详细地写出了函数的规则。不知道这样引用人家的文章行不行

下面是一点简单的猜测

因为关于sort()的排序规则我无从得知,所以以下结论仅通过操作得到的规律中发现。

首先sort的书写规则应该是这样的:

listx.sort( key = , reverse = )

  • 因为是列表所以暂时不需要key。reverse默认值是False。请注意F要大写。

那么,先来看一下以下几种情况:

list0 = [0,1,2,3,4]                         #number:integer
list1 = ['1','2','3','4','5']               #number:string
list2 = ['a','b','c','d','e']               #letter:string
list3 = ['a','blake','c','d','e']           #letter&word:string
list4 = ['99','b','cyka blyat',' d','11']   #number&letter&word:string

list0.sort(reverse = True)                  #倒序排列(大 → 小)
list1.sort(reverse = True)
list2.sort(reverse = False)                 #顺序排列(小 → 大)
list3.sort(reverse = True)
list4.sort(reverse = True)


print(list0)
print(list1)
print(list2)
print(list3)
print(list4)
  • 请注意list4中的’空格d’,不是’d’ 我草这个编辑器有毒

那么运行,得到结果:

在这里插入图片描述
可以看出:

  • list0 ,list1采用倒序排列,冇闷台
  • list2采用顺序排列,no趴笨

那么暂时通过以上三个列表得出的结论可以认为是:

sort()按照列表元素的下标/索引进行排序。

似乎也许大概八成好像差不多不一定 没错但是:

list4中,

扫描二维码关注公众号,回复: 9127255 查看本文章

[‘99’,‘b’,‘cyka blyat’,’ d’,‘11’]

那么理论上如果按照下标排序,得到的结果应该是

[‘11’,‘d’,‘cyka blyat’, ’ b’,‘99’]

但是实际上得到

[‘cyka blyat’, ‘b’, ‘99’, ‘11’, ’ d’]

所以我推了推眼镜 认为:

  • sort()应该是按照 列表中每一个元素的 首字符 的ascii码值 排列的。
    那么这样一来的话,把list4中每个元素的首字符的ascii码写出来就是这样子:

[57,98,99,32,49]

那么,理论上来说list4排列的结果就和[ ‘9’,‘b’,‘c’,‘ ’,‘1’ ]是一样的。

list4 = ['99','b','cyka blyat',' d','11']
list5 = ['9','b','c',' ','1']

list4.sort(reverse = True)
list5.sort(reverse = True)

print(list4)
print(list5)

在这里插入图片描述

顺带一提大写字母和小写字母的ascii码值是不一样的,A = 65,a = 97,差值是32

以上就是我的全部 意淫 脑补 科学考察 过程。

(不保证正确性)

发布了6 篇原创文章 · 获赞 7 · 访问量 1886

猜你喜欢

转载自blog.csdn.net/bluenessdrops/article/details/104297309