On some operation functions of list

Posted by rogair on Thu, 13 Feb 2020 10:36:22 +0100

this is a note

l0 = 'acoilia.kinlrmi.3000.clevorihno'
l1 = l0.split('.')  #List is variable type
print(l1)			#li it's a list now

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)

You need to verify which function takes it off

Here's a simple guess

Because I don't know the sorting rules of sort(), the following conclusions are only found in the rules obtained by operation.

First of all, the writing rules of sort should be as follows:

listx.sort( key = , reverse = )

  • Because it's a list, you don't need a key at this time. reverse defaults to False. Notice that F is capitalized.

First, let's look at the following situations:

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)                  #Reverse order (large → small)
list1.sort(reverse = True)
list2.sort(reverse = False)                 #Sequence (small → large)
list3.sort(reverse = True)
list4.sort(reverse = True)


print(list0)
print(list1)
print(list2)
print(list3)
print(list4)
  • Please note that the 'space d' in list4 is not 'd' and this editor is poisonous

Then run and get the result:


It can be seen that:

  • list0 and list1 are arranged in reverse order, with no closed platform
  • list2 is arranged in order, no

The conclusion drawn from the above three lists can be considered as follows:

sort() sorts by the subscript / index of the list element.

Maybe it's probably not necessarily true, but:

In list4,

['99','b','cyka blyat',' d','11']

In theory, if you rank by subscript, the result should be

['11','d','cyka blyat', ' b','99']

But in fact

['cyka blyat', 'b', '99', '11', ' d']

So I pushed my glasses and thought:

  • sort() should be arranged according to the ascii code value of the first character of each element in the list.
    In this way, the ascii code of the first character of each element in list4 is written as follows:

[57,98,99,32,49]

In theory, then, the result of list4 is the same as ['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)

Incidentally, the ascii value of capital letters and lowercase letters is different, A = 65,a = 97, and the difference is 32

That's all my research on mental health.

(no guarantee of correctness)

Published 6 original articles, won praise 7, visited 1758
Private letter follow

Topics: ascii