Examples Python3 (V)

Flip Python list

Define a list and turn it.

For example, a swap first and third elements:

Front flips: List = [10, 11, 12, 13, 14, 15]
flip: [15, 14, 13, 12, 11, 10]
Example 1

def Reverse(lst):
return [ele for ele in reversed(lst)]

= LST [10,. 11, 12 is, 13 is, 14, 15]
Print (Reverse (LST))
Examples of the above output:

[15, 14, 13, 12, 11, 10]
Example 2

def Reverse(lst):
lst.reverse()
return lst

= LST [10,. 11, 12 is, 13 is, 14, 15]
Print (Reverse (LST))
Examples of the above output:

[15, 14, 13, 12, 11, 10]
Example 3

def Reverse(lst):
new_lst = lst[::-1]
return new_lst

= LST [10,. 11, 12 is, 13 is, 14, 15]
Print (Reverse (LST))
Examples of the above output:

[15, 14, 13 is, 12 is,. 11, 10]
the Python determines whether there is an element in the list

Define a list, and determines whether the elements in the list.

Example 1

test_list = [ 1, 6, 3, 5, 3, 4 ]

print ( "View 4 is in the list (using loop):")

for i in test_list:
if(i == 4) :
print ("存在")

print ( "View 4 is in the list (using the keyword in):")

IF (in test_list. 4):
Print ( "present")
Examples of the above output:

4 Check whether (using loops) in the list:
the presence of
View 4 is in the list (using the keyword in):
the presence
EXAMPLE 2

from bisect import bisect_left

Initialization list

test_list_set = [ 1, 6, 3, 5, 3, 4 ]
test_list_bisect = [ 1, 6, 3, 5, 3, 4 ]

print ( "View 4 is in the list (using the set () + in):")

test_list_set = set(test_list_set)
if 4 in test_list_set :
print ("存在")

print ( "View 4 is in the list (use the sort () + bisect_left ()):")

test_list_bisect.sort ()
IF bisect_left (test_list_bisect,. 4):
Print ( "present")
Examples of the above output:

View 4 is in the list (using the set () + in):
the presence of
View 4 is in the list (use the sort () + bisect_left ()) :
the presence of
Python Clear List

Define a list, and the list of empty, can clear () methods.

Example 1

= RUNOOB [. 6, 0,. 4,. 1]
Print ( 'before blanking:', RUNOOB)

RUNOOB.clear ()
Print ( 'after emptying:', RUNOOB)
above example output is:

Before blanking: [6, 0, 4, 1]
after emptying: []
the Python replication list

Define a list, and to copy the list to another list element.

Example 1

def clone_runoob(li1):
li_copy = li1[:]
return li_copy

= in Li1 [. 4,. 8, 2, 10, 15, 18 is]
LI2 = clone_runoob (in Li1)
Print ( "original list:", in Li1)
Print ( "the replication list:", li2)
examples above are output:

Original list: [4, 8, 2, 10, 15, 18]
to copy the list: [4, 8, 2, 10, 15, 18]
Example 2: extend () method

def clone_runoob(li1):
li_copy = []
li_copy.extend(li1)
return li_copy

= in Li1 [. 4,. 8, 2, 10, 15, 18 is]
LI2 = clone_runoob (in Li1)
Print ( "original list:", in Li1)
Print ( "the replication list:", li2)
examples above are output:

Original list: [4, 8, 2, 10, 15, 18]
to copy the list: [4, 8, 2, 10, 15, 18]
Example 3: Using the list () method

def clone_runoob(li1):
li_copy = list(li1)
return li_copy

= in Li1 [. 4,. 8, 2, 10, 15, 18 is]
LI2 = clone_runoob (in Li1)
Print ( "original list:", in Li1)
Print ( "the replication list:", li2)
examples above are output:

Original list: [4, 8, 2, 10, 15, 18]
[4, 8, 2, 10, 15, 18]: a list of the copy
number of Python computation elements appear in the list

Define a list, and count the number of an element that appears in the list.

E.g:

Input: LST = [15,. 6,. 7, 10, 12 is, 20 is, 10, 28, 10]
X 10 =
Output: 3
Example 1

def countX(lst, x):
count = 0
for ele in lst:
if (ele == x):
count = count + 1
return count

= LST [. 8,. 6,. 8, 10,. 8, 20 is, 10,. 8,. 8]
X =. 8
Print (countX (LST, X))
above example output is:

5
Example 2: count () method

def countX(lst, x):
return lst.count(x)

= LST [. 8,. 6,. 8, 10,. 8, 20 is, 10,. 8,. 8]
X =. 8
Print (countX (LST, X))
above example output is:

5
Python list of elements of the calculation and

Define a list of numbers, and calculates the sum of list elements.

For example: Enter: [12, 15, 3, 10] Output: 40

Example 1

total = 0

list1 = [11, 5, 17, 18, 23]

for ele in range(0, len(list1)):
total = total + list1[ele]

( "list element is the sum of:", total) print
example of the output result of the above is:

And elements of the list is: 74
Example 2: while () loop

total = 0
ele = 0

list1 = [11, 5, 17, 18, 23]

while (he <len (list1)):
Total = Total + list1 [it]
it + 1 =

( "list element is the sum of:", total) print
example of the output result of the above is:

And elements of the list is: 74
Example 3: recursive

list1 = [11, 5, 17, 18, 23]

def sumOfList(list, size):
if (size == 0):
return 0
else:
return list[size - 1] + sumOfList(list, size - 1)

total = sumOfList (list1 only (list1))

( "list element is the sum of:", total) print
example of the output result of the above is:

And elements of the list as: 74
the Python computes the product of list elements

Define a list of numbers, and computes the product of list elements.

E.g:

Input: list1 = [1, 2, 3]
Output: 6
Calculated: 1 2 . 3
Example 1

def multiplyList(myList) :

result = 1
for x in myList: 
     result = result * x  
return result  

= List1 [. 1, 2,. 3]
List2 = [. 3, 2,. 4]
Print (multiplyList (List1))
(multiplyList (List2)) Print
examples above are output:

624
Python Find the smallest element of the list

Define a list of numbers, and find the smallest element in the list.

E.g:

Input: list1 = [10, 20, 4]
Output: 4
Example 1

list1 = [10, 20, 4, 45, 99]

list1.sort()

print ( "minimum element:", * list1 [: 1 ])
above example output is:

The minimum elements: 4
Example 2: min () method

list1 = [10, 20, 1, 45, 99]

print ( "minimum element:", min (list1))
above example output is:

Minimum elements: 1
Python Find the largest element in the list

Define a list of numbers, and find the largest element in the list.

E.g:

Input: list1 = [10, 20, 4]
Output: 20
Example 1

list1 = [10, 20, 4, 45, 99]

list1.sort()

print ( "Maximum elements:", list1 [-1])
above example output is:

Maximum elements: 99
Example 2: max () method

list1 = [10, 20, 1, 45, 99]

print ( "Maximum elements:", max (list1))
Examples of the above output:

The largest element is: 99
Python Removes the specified position of the character string

Given a string, and then remove the character to develop positions:

Examples

test_str = "Runoob"

Printing original string

print ( "Original string:" + test_str)

Removing the third character n

new_str = ""

for i in range(0, len(test_str)):
if i != 2:
new_str = new_str + test_str[i]

print ( "after removal of string:" + new_str)
execute code above, the output is:

Original string: Runoob
after removing the string is: Ruoob
notes

Reference Method:

test_str = "Runoob"
new_str = test_str.replace(test_str[3], "", 1)
print(new_str)

Well, I gave everyone here to share the end of the text to share a Bo Fuli

Examples Python3 (V)

Examples Python3 (V)

Access: group 839 383 765 plus python can get!

Guess you like

Origin blog.51cto.com/14186420/2406371