Friday, July 29, 2022

Are there any Python tuple methods?

The tuple methods can be easily found by creating a tuple and using the dir built-in function.
test = ("test1", "test2")
print("dir(test) = {}".format(dir(test)))
Output:
dir(test) = ['__add__', '__class__', '__class_getitem__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'count', 'index']
From all of these built-in methods the last two in previous list are used on tuples and these are: count(), and index(). The count() is a built-in method that returns the number of times a specified value occurs in tuple. The index() is a built-in method that searches the tuple for a specified value and returns the position of where it was found.
Example 1 Create a list containing 100 elements that are random integers in range from 0 to 10 (including 10). Then convert the list into a tuple and develop a procedure for creating a list of unique tuple elements. Then using the count tuple built-in method print out the number of times each element (number) in a tuple occurs. Solution First step: Create a list of 100 random integers in range from 0 to 10 To do this we have to import the random library.
import random
Now we have to create a list of 100 random integers in the range from 0 to 10 (including 10). To do this we will need the random randint function.
testList = [random.randint(0,10) for i in range(0,100,1)]
To see if we have generated the list of 100 random integers we will print the testList.
print("testList = {}".format(testList))
Output:
testList = [2, 9, 1, 4, 1, 7, 7, 7, 10, 6, 3, 1, 7, 0, 6, 6, 9, 0, 7, 4, 3, 9, 1, 5, 0, 0, 0, 10, 8, 0, 6, 10, 3, 6, 0, 8, 3, 7, 7, 8, 3, 5, 3, 10, 3, 7, 4, 0, 6, 8, 10, 1, 2, 10, 4, 1, 5, 8, 6, 8, 10, 3, 4, 4, 9, 7, 8, 6, 9, 0, 7, 3, 6, 6, 10, 2, 5, 8, 10, 5, 1, 7, 10, 8, 1, 2, 8, 6, 5, 7, 0, 7, 0, 4, 9, 9, 9, 6, 10, 2]
It should be noted since we are using the randint function from a random library the list of generated integers might differ from the sequence in this example. To convert the list to a tuple we will use the tuple function and verify if the list is converted to a tuple we will use the type built-in function.
testTuple = tuple(testList)
print("testTuple = {}".format(testTuple))
print("type(testTuple) = {}".format(type(testTuple)))
Output:
testTuple = (2, 9, 1, 4, 1, 7, 7, 7, 10, 6, 3, 1, 7, 0, 6, 6, 9, 0, 7, 4, 3, 9, 1, 5, 0, 0, 0, 10, 8, 0, 6, 10, 3, 6, 0, 8, 3, 7, 7, 8, 3, 5, 3, 10, 3, 7, 4, 0, 6, 8, 10, 1, 2, 10, 4, 1, 5, 8, 6, 8, 10, 3, 4, 4, 9, 7, 8, 6, 9, 0, 7, 3, 6, 6, 10, 2, 5, 8, 10, 5, 1, 7, 10, 8, 1, 2, 8, 6, 5, 7, 0, 7, 0, 4, 9, 9, 9, 6, 10, 2)
type(testTuple) = <class 'tuple'>

Procedure for finding unit tuple elements. The procedure for finding unique elements in a list or tuple is very simple. An empty list will be created and then used when traversing a tuple i.e. each time the new element in the tuple is found it will be appended to the empty list.
uniqueList = []
for x in testTuple:
if x not in uniqueList:
uniqueList.append(x)
uniqueList.sort()
print("uniqueList = {}".format(uniqueList))
The previous code will traverse over the testTuple. Each time the element from the testTuple is not found in the uniqueList it will be appended to uniqueList. After the tuple is traversed the uniqueList will be sorted using sort() built-in function and finally, the list will be shown as output using the print function.
Output:
uniqueList = [0,1,2,3,4,5,6,7,8,9,10]
Final part: print out the number of times each element (number) in tuple occurs.
for i in range(len(uniqueList)):
print("""The number {} occurs {} times in testTuple.""".format(uniqueList[i],\
testTuple.count(uniqueList[i]))
Output:
The number 0 occurs 11 times in a testTuple.
The number 1 occurs 8 times in a testTuple.
The number 2 occurs 5 times in a testTuple.
The number 3 occurs 9 times in a testTuple.
The number 4 occurs 7 times in a testTuple.
The number 5 occurs 6 times in a testTuple.
The number 6 occurs 12 times in a testTuple.
The number 7 occurs 13 times in a testTuple.
The number 8 occurs 10 times in a testTuple.
The number 9 occurs 8 times in a testTuple.
The number 10 occurs 11 times in a testTuple.
The entire code for this example is given below.
import random
testList = [random.randint(0,10) for i in range(0,100,1)]
print("testList = {}".format(testList))
testTuple = tuple(testList)
print("testTuple = {}".format(testTuple))
print("type(testTuple) = {}".format(type(testTuple)))
uniqueList = []
for x in testTuple:
if x not in uniqueList:
uniqueList.append(x)
uniqueList.sort()
print("uniqueList = {}".format(uniqueList))
for i in range(len(uniqueList)):
print("""The number {} occurs {} times in a testTuple.""".format(uniqueList[i],\
testTuple.count(uniqueList[i])))
Entire output:
testList = [2, 9, 1, 4, 1, 7, 7, 7, 10, 6, 3, 1, 7, 0, 6, 6, 9, 0, 7, 4, 3, 9, 1, 5, 0, 0, 0, 10, 8, 0, 6, 10, 3, 6, 0, 8, 3, 7, 7, 8, 3, 5, 3, 10, 3, 7, 4, 0, 6, 8, 10, 1, 2, 10, 4, 1, 5, 8, 6, 8, 10, 3, 4, 4, 9, 7, 8, 6, 9, 0, 7, 3, 6, 6, 10, 2, 5, 8, 10, 5, 1, 7, 10, 8, 1, 2, 8, 6, 5, 7, 0, 7, 0, 4, 9, 9, 9, 6, 10, 2]
testTuple = (2, 9, 1, 4, 1, 7, 7, 7, 10, 6, 3, 1, 7, 0, 6, 6, 9, 0, 7, 4, 3, 9, 1, 5, 0, 0, 0, 10, 8, 0, 6, 10, 3, 6, 0, 8, 3, 7, 7, 8, 3, 5, 3, 10, 3, 7, 4, 0, 6, 8, 10, 1, 2, 10, 4, 1, 5, 8, 6, 8, 10, 3, 4, 4, 9, 7, 8, 6, 9, 0, 7, 3, 6, 6, 10, 2, 5, 8, 10, 5, 1, 7, 10, 8, 1, 2, 8, 6, 5, 7, 0, 7, 0, 4, 9, 9, 9, 6, 10, 2)
type(testTuple) = <class 'tuple'>
unqiueList = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
The number 0 occurs 11 times in a testTuple.
The number 1 occurs 8 times in a testTuple.
The number 2 occurs 5 times in a testTuple.
The number 3 occurs 9 times in a testTuple.
The number 4 occurs 7 times in a testTuple.
The number 5 occurs 6 times in a testTuple.
The number 6 occurs 12 times in a testTuple.
The number 7 occurs 13 times in a testTuple.
The number 8 occurs 10 times in a testTuple.
The number 9 occurs 8 times in a testTuple.
The number 10 occurs 11 times in a testTuple.



Example 2 Create a program that will show as output index of tuple elements. However, the tuple and list with elements that will be used in the search are randomly generated using a random library i.e. the randint function.
Solution: To solve this problem first we have to import the random library, and then we have to create two lists. One list will be converted to a tuple while the other will be used for searching through a tuple. For this example, the range of integers for the baselist i.e. baseTuple will be from 0 to 100 and the length of the list will be 100. The second list named ListQuerry will consist of randomly generated integers in the range from 0 to 100 and the length of the list will be 100.
import random
BaseTuple = tuple([random.randint(0,100) for i in range(0,100,1)])
ListQuerry = [random.randint(0,100) for i in range(0,10,1)]
print("BaseTuple = {}".format(BaseTuple))
print("ListQuerry = {}".format(ListQuerry))
Output:
BaseTuple = (17, 72, 97, 8, 32, 15, 63, 97, 57, 60, 83, 48, 100, 26, 12, 62, 3, 49, 55, 77, 97, 98, 0, 89, 57, 34, 92, 29, 75, 13, 40, 3, 2, 3, 83, 69, 1, 48, 87, 27, 54, 92, 3, 67, 28, 97, 56, 63, 70, 29, 44, 29, 86, 28, 97, 58, 37, 2, 53, 71, 82, 12, 23, 80, 92, 37, 15, 95, 42, 92, 91, 64, 54, 64, 85, 24, 38, 36, 75, 63, 64, 50, 75, 4, 61, 31, 95, 51, 53, 85, 22, 46, 70, 89, 99, 86, 94, 47, 11, 56)
ListQuerry = [84, 65, 13, 99, 20, 66, 50, 47, 62, 93]
The second step is to create for loop with the if-else statement. The other approach would be to create a while loop with try and except since the index built-in function returns ValueError if the element is not found in the tuple. However, in this case, we have chosen the for loop with the if-else statement. The for loop will traverse over the ListQuerry list and search in tuple if an element exists. If the element exists then the index function will return the index number at which the tuple element is located in the tuple. On the other hand, if the element from ListQuerry does not exist in the BaseTuple then the program will show as output the string for example "The element 84 is not in the BaseTuple".
for i in range(len(ListQuerry)):
if ListQuerry[i] in BaseTuple:
print("The element {} is at index {} in BaseTuple".format(ListQuerry[i],\
BaseTuple.index(ListQuerry[i])))
else:
print("The element {} is not in the BaseTuple".format(ListQuerry[i]))
Output:
The element 84 is not in the BaseTuple
The element 65 is not in the BaseTuple
The element 13 is at index 29 in BaseTuple
The element 99 is at index 94 in BaseTuple
The element 20 is not in the BaseTuple
The element 66 is not in the BaseTuple
The element 50 is at index 81 in BaseTuple
The element 47 is at index 97 in BaseTuple
The element 62 is at index 15 in BaseTuple
The element 93 is not in the BaseTuple

No comments:

Post a Comment