Python. Converting data structure (sets, strings, lists, tuples)

11.2.4.2 convert from one data structure to another

Python. Converting data structure (sets, strings, lists, tuples)

String to List to String

A string is an immutable data type, so to replace one character, you can convert to a list, perform the replacement, and get a string back.

word = "caw"
word[1] = "o"  # TypeError: 'str' object does not support item assignment
lst = list(word)  # convert string to list
print(lst)        # output list ["c", "a", "w"]
lst[1] = "o"      # change the second item of the list
print(lst)        # output list ["c", "o", "w"]
word = "".join(lst)  
print(word)       # output string cow

String to List to String

word = “My favourite subject is programming"
lst = word.split()    # split string by space
print(lst)    # output ["My", "favourite", "subject", "is", "programming"]
word = "-".join(lst) # get string from list 
print(word)          # output string My-favourite-subject-is-programming

Tuple to List to Tuple

A tuple is an immutable data type, so to replace one element, you can convert to a list, perform the replacement, and get a tuple back.

tup = ("Amir", 5)
tup[1] = 4   # TypeError: 'tuple' object does not support item assignment
lst = list(tup)    # convert tuple to list
lst[1] = 4         # change the second item of list to 4
tup = tuple(lst)   # convert list to tuple
print(tup)         # output ("Amir", 4)

List to Set to List

Anar visited the capitals of the world "Nur-Sultan", "Moscow", "Baku", "Ankara", "Moscow" during the year. How many different capitals has Anar visited?

capitals = ["Nur-Sultan", "Moscow", "Baku", "Ankara", "Moscow"]
print(len(capitals))              # output 5 
unique_capitals = set(capitals)   # convert list to set
capitals = list(unique_capitals)  # convert set to list
print(capitals)       # output ["Nur-Sultan", "Moscow", "Baku", "Ankara"]
print(len(capitals))              # output 4

Tuple to Set to Tuple

capitals = ("Nur-Sultan", "Moscow", "Baku", "Ankara", "Moscow")
print(len(capitals))               # output 5 
unique_capitals = set(capitals)    # convert tuple to set
capitals = tuple(unique_capitals)  # convert set to tuple
print(capitals)       # output ("Nur-Sultan", "Moscow", "Baku", "Ankara")
print(len(capitals))               # output 4

Set to List

We can convert an unordered data structure to an ordered one and apply processing to indexed items.

capitals = {"Nur-Sultan", "Moscow", "Baku", "Ankara"}
lst = list(capitals)      # convert set to list
print(lst)                # ["Nur-Sultan", "Moscow", "Baku", "Ankara"]

Questions:

Exercises:

Ex. 1 Python. Collections. (Author: Mr. Halil Mali - international teacher of NIS Uralsk)

Ex. 2 Quiz. Python. Collections. (Author: Mr. Halil Mali - international teacher of NIS Uralsk)

 

Категория: Programming languages | Добавил: bzfar77 (30.11.2021)
Просмотров: 189 | Теги: Python, Set, List, data structure, tuple, String | Рейтинг: 5.0/1
Всего комментариев: 0
avatar