Python operation in set function, list

A, set function

set () function creates an unordered set of elements is not repeated, the relationship can be tested, deduplication may also be calculated intersection, difference, and sets the like.

example:

1, set gets the intersection (&), difference (-), and set (|)

SET = S1 ( 'here Wallpaper')
S2 = SET ( 'Hello')
Print (S1-S2) # difference set
print (s1 & s2) # intersection
print (s1 | s2) # union

result:

 

2, delete duplicates elements

(1) using the set function

S = {"python","apple","123","123","a"}
print(set(S))

operation result:

Second, the action list

(A), the concept of the list

List (list) containing an ordered sequence of zero or more objects referenced by sequences belonging to the type. Tuples of different length and content list are variable, you can increase the content list, delete or replace. There are no length limit, or different types of elements, using very flexible. List with the brackets [] indicates, can also list () function to convert the string into a tuple or list. Direct use list () function returns an empty list.

(B), a list of the type of operation

List is a sequence type, the following description will be given of several conventional methods or functions:

Function or method description
ls[i]=x Ls alternate list of data items i x
ls[i:j]=lt Ls lt alternate list with a list of data items i to j
ls[i:j:k]=lt Replace the first item in the list ls i to j to k is in the data list the number of steps lt
ls the [i: j] Deleted from the list of data items i to j is equivalent to ls [i: j] = []
del ls[i:j:k] Delete list item i to j to k is the number of steps of data
or ls + = lt ls.extend (lt) The elements in the list lt ls added to the list of
ls = n * Update the list ls, elements repeated n times
ls.append(x) In the list of the last ls add an element x
ls.clear() Delete all the elements in ls
ls.copy() Generate a new list, copy all the elements in ls
ls.insert(i,x) I-th position x increases in the list ls
ls.pop(i) Remove the list item i element and remove the element
ls.remove(x) The x elements in the list the first occurrence of deletion
ls.reverse(x) Reverse elements in the list

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Code analysis:

List = [ ' Apple ' , ' Hello ' , ' Q ' , ' Ben ' , ' Hello ' ]
 # output list 
Print (List)
 # replacing i-th item in the list is X 
List [0] = ' Fruit ' 
Print ( list)
 # is replaced with the i-th list ls to the j-th data item in list of 
ls = [ ' a ' , ' H ' , ' S ' ] 
list [0:2] = ls
 print (list)
# Replaced with ls list in list of data items i to j k is the number of steps of 
[0: 2:. 1] = list ls
 Print (list)
 # delete list of data items i to j is equivalent to ls [i : J] = [] 
del list [0: 2 ]
 # list [0: 2] = [] 
Print (list)
 # Finally add an element x in the list ls 
list.append ( ' D ' )
 Print (list)
 # ls delete all the elements in 
list.clear ()
 Print (List)

operation result:

['apple', 'hello', 'q', 'ben', 'hello']
['fruit', 'hello', 'q', 'ben', 'hello']
['a', 'h', 's', 'q', 'ben', 'hello']
['a', 'h', 's', 's', 'q', 'ben', 'hello']
['s', 's', 'q', 'ben', 'hello']
['s', 's', 'q', 'ben', 'hello', 'd']
[]

课后习题及例题解答:

1、列表ls=[2,5,7,1,6],请按照升序降序排列

ls = [2,5,7,1,6]
# 升序
ls.sort()
s1 = sorted(ls)
# s1 = sorted(ls,reverse=False)
print(ls)
print(s1)
# 降序
s2 = sorted(ls,reverse=True)
print(s2)

2、ls1=[1,43],ls2=ls1,ls1[0]=22,计算并思考两列表的运行结果

ls1=[1,43]
ls2=ls1
ls1[0]=22
print(ls1,ls2)

3、ls=[[2,3,7],[[3,5],25],[0,9]],求len(ls)

ls=[[2,3,7],[[3,5],25],[0,9]]
print(len(ls))

4、从lst1中去除两个列表的交集元素

lst1 = set('there')
lst2 = set('hello')
lst = lst1&lst2
for x in lst:
    lst1.remove(x)
print(lst1)

5、把lst1=[1,2,3,4]中的奇数位与lst2=[5,6,7,8]中的偶数位,求并集

lst1=[1,2,3,4]
ls1 = []
ls2 = []
for x in lst1:
    if x%2 != 0:
        ls1.append(x)
lst2=[5,6,7,8]
for y in lst2:
    if y%2 == 0:
        ls2.append(y)
S = list(set(ls1).union(set(ls2)))
print(sorted(S))

 

以上5题目运行结果:

[1, 2, 5, 6, 7]
[1, 2, 5, 6, 7]
[7, 6, 5, 2, 1]
[22, 43] [22, 43]
3
{'t', 'r'}
[1, 3, 6, 8]

6、删除列表中的重复元素

lst1 = ['d','df','df']
lst2 = []
for x in lst1:
    if x not in lst2:
        lst2.append(x)
print(lst2)

结果:

三、总结

列表是一个十分灵活的数据结构,它具有处理任意长度、混合数据类型的能力,并提供了丰富的基础操作符和方法。

Guess you like

Origin www.cnblogs.com/CJR-QYF/p/11738277.html