Get the App
SLTechnology News&Howtos  ›  Development  › 

The combination of basic types of connections and the way they convert to each other in Python

Shulou Source: shulou.com Published: 2022-06-03 12:37:59 09月20日 Update

This article introduces the relevant knowledge of "the combination of basic types of connections and the way they convert each other in Python". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The update method is very common.

1. Concatenate two strings

A = "hello" b = "world" a + = b print (a) # hello world

two。 The connection of the dictionary

Dict1 = {1: "a", 2: "b"} dict2 = {3: "c", 4: "d"} dict1.update (dict2) print (dict1) # {1:'a', 2: 'baked, 3:' C', 4:'d'}

3. Connection to the list

List1 = [1,2,3] list2 = [4,5,6] list1.extend (list2) # [1,2,3,4,5,6] print (list1)

4. Connection of tuples

Tuple1 = (1,2) tuple2 = (3,4) tuple1 + = tuple2 print (tuple1) # (1,2,3,4)

5. Convert dictionaries to strings

Dict1 = {1: "a", 2: "b"} str1 = str (dict1) print (str1) # {1:'a', 2:'b'} print (type (str1)) #

6. Convert dictionaries to lists

Dict1 = {1: "a", 2: "b"} list1 = list (dict1.keys ()) list2 = list (dict1.values ()) list3 = list (dict1) print (list1) # [1mai 2] print (list2) # ['ajar,' b'] print (list3) # [1J2]

7. Convert dictionary to tuple

Dict1 = {1: "a", 2: "b"} tuple1 = tuple (dict1.keys ()) tuple2 = tuple (dict1.values ()) tuple3 = tuple (dict1) print (tuple1) # (1,2) print (tuple2) # ('await,' b') print (tuple3) # (1,2)

8. Convert list to string

List1 = [1,2,3] str1 = str (list1) print (str1) # [1,2,3] print (type (str1)) #

9. Convert a list to a dictionary

# 1. List1 = [1, 2, 3] list2 = ["a", "b", "c"] dict1 = dict (zip (list1, list2)) print (dict1) # {1: 'for i in list1, 2:' baked, 3:'c'} # 2. Dict1 = {} for i in list1: dict1 [I] = list2 [list1.index (I)] print (dict1) # {1: 'ajar, 2:' b' 3:'c'} # 3. List1 = [[1,'a'], [2,'b'], [3,'c']] dict1 = dict (list1) print (dict1) # {1:'a', 2: 'baked, 3:' c'}

10. Convert list to tuple

List1 = [1,2,3] tuple1 = tuple (list1) print (tuple1) # (1,2,3)

11. Convert tuples to strings

Tuple1 = (1,2,3) str1 = tuple (tuple1) print (str1) # (1,2,3) print (type (str1)) #

twelve。 Convert tuples to dictionaries

# 1. Tuple1 = (1,2,3) tuple2 = (4,5,6) dict1 = dict (zip (tuple1, tuple2)) print (dict1) # {1: 4,2: 5,3: 6} # 2 dict1 = {} for i in tuple1: dict1 [I] = tuple2 [tuple1.index (I)] print (dict1) # {1: 4,2: 5,3: 6} # tuple1 = (1,2) tuple2 = (4,5) tuple3 = (tuple1 Tuple2) dict1 = dict (tuple3) print (dict1) # {1: 2, 4: 5}

13. Convert tuples to lists

Tuple1 = (1,2) list1 = list (tuple1) print (list1) # [1,2] "combination of basic types of connections and ways to convert each other in Python" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Tags: Dictionaries characters strings methods types combinations content more knowledge practicality achievement and then two dilemmas practice common use situations articles methods cases Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Tech Info NVidia macOS Shulou Information