程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python Development -- 14 tuple type built-in method

編輯:Python

List of articles

    • One . effect
      • 1. Tuples and lists
      • 2. The function of tuples
    • Two . Define the way
      • Tuple immutable parsing
    • 3、 ... and . Common operations + Built-in methods
    • Priority operation (*********)
      • 1. Value by index ( Take... Forward + Take... In reverse ) : Can only take
      • 2. section ( Head and tail + step )
      • 3、 length : .len()
      • 4、 Members of the operation int and not in
      • 5、 loop
    • Operations that need to be mastered (****)
      • 1. Number of Statistics : .count()
      • 2. Search index : .index()
    • Four . summary
      • You can store multiple values
      • Orderly , Depends on index values
    • Four . summary
      • You can store multiple values
      • Orderly , Depends on index values
      • Immutable type ---> can hash type

One . effect

1. Tuples and lists

  • Tuple is actually immutable A list of
  • The list is readable and modifiable , And tuple read-only cannot be changed
  • Under the same data type , Tuples are more space efficient , And more efficient than lists
  • Because the bottom layer of tuple only provides read mechanism , The list has both read and change mechanism

2. The function of tuples

  • Obvious , It also stores multiple values by location , The index corresponds to the value

Two . Define the way

  • stay “( )” Use commas to separate multiple elements of any type
  • If the tuple contains only one element , So it needs to be separated by commas : (111,)
  • Tuple immutability means first floor Elemental The memory address cannot be changed
  • If the first layer contains a sublist , Then the memory address of the sublist cannot be changed , But you can change the elements in the sublist ( List variable types )
  • “ factory ” : tuple
# Definition 
l=(11,11.11,"aaa",[222,333]) # Back call l=tuple(...)
# Defining a single value requires a comma ( No, it's just the meaning of inclusion )
x = (18)
y = (18,)
print(type(x)) #<class 'int'>
print(type(y)) #<class 'tuple'>
# A tuple 
j=(1,) # Add a comma 
print(j,type(j))
(1,) <class 'tuple'>
  • Tuple immutable parsing

# Immutable type resolution of tuples 
tup = (111,"aaa",[222,333])
print(id(tup[0]),id(tup[1]),id(tup[2]))
#140709693587136 2763387504816 2763386278472
tup[0] = 222 # Report errors 
tup[1] = 333 # Report errors 
tup[2] = 333 # Report errors 
# When changing elements in a sublist 
tup[2][0] = 333
print(tup[2]) #[333, 333]
# Check after the modification , Of the first element of a tuple "id" There is no change 
print(id(tup[0]),id(tup[1]),id(tup[2]))
#140709693587136 2763387504816 2763386278472

3、 ... and . Common operations + Built-in methods

Priority operation (*********)

1. Value by index ( Take... Forward + Take... In reverse ) : Can only take

tup = (111,222,333,444,555)
# Take... Forward 
print(t[0]) #111
# Take... In reverse 
print(t[-1]) #555

2. section ( Head and tail + step )

tup = (111,222,333,444,555,666)
# Head and tail 
print(t[0:3]) # (111,222,333)
# reverse 
tup2 = tup[::-1]
print(tup2) #(666,555,444,333,222,111)

3、 length : .len()

tup = (111,222,333,444,555,666)
print(len(tup)) # 6

4、 Members of the operation int and not in

  • Judge whether or not Just on the first floor In the elements
tup = (111,"aaa",[222,333])
print(111 in tup) #True
print(222 in tup) #False
print([222,333] in tup) #True

5、 loop

tup = (111,"aaa",[222,333])
for i in tup:
print(i)
# 111
# aaa
# [222, 333]

Operations that need to be mastered (****)

1. Number of Statistics : .count()

  • Look at the number of elements in a tuple , It's just the first floor
tup = (111,"aaa",222,222,[222,333])
print(tup.count(222)) # 2

2. Search index : .index()

  • Find the index of the specified character in the list , You can specify a range to find
  • There's nothing in tuples like in strings : find、rfind、rindex These methods of
  • Just look up on the first level
tup = (111,"aaa",222[222,333])
print(tup.index("aaa",1,3)) # 1

Four . summary

  • You can store multiple values

  • Orderly , Depends on index values

Just look up on the first level

tup = (111,"aaa",222[222,333])
print(tup.index("aaa",1,3)) # 1

Four . summary

  • You can store multiple values

  • Orderly , Depends on index values

  • Immutable type —> can hash type


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved