A group of Python interview questions encountered during a previous interview, albeit a simple but special basis.
1.Python How can I tell if an integer is included in a two-dimensional array?
Array = [[1, 3, 5.6, 7.2, 8, 9.9], [2.5, 4.7, 6.8, 7.3, 9, ten], [3.7, 6.7, 9.8, 9.9, one, 12.1]] Array: for in i: if str (j). IsDigit (): # Determines whether an integer can also be passed by Isinstance (j, int)
Print (the " two-dimensional list contains integers " break
2. How do I turn a space in a string into a%20?
#1. By replacing the method of implementation>>> str =" How does do">>> Str.replace (" ","%20")'How%20do%20you%20do'#2. By encoding the way to achieve>>> fromUrllibImportParse>>>parse.quote (str)'How%20do%20you%20do'
3. Delete duplicate elements of the list?
# by set the way to first go to heavy, in conversion to list >>> list1 = [1, 3, 3, 4, 1, 2]>>> list (set (List1)) [1, 2, 3 , 4]
If the first question turns into a 4-dimensional array, and then again to determine whether to include integers, here we need to consider generating arrays;
Array = [] for in range (0, 4): = [] for in Range (1, 5): array1.append (i) array.append (array1)print(array)
Through two for loops can be generated, although simple we still meet the requirements, the following in the increase of the requirements of the 4 of the array, but the two-dimensional array is required to contain integers and include decimals;
ImportRandomarray= [] forJinchRange (0, 4): Array1= [] forIinchRange (0, 4): Radnum= Random.randint (0, 100) ifRadnum% 2 = = 0:#If an even number is generated between 1 and 10 integers, if the cardinality is generated by a decimal, and 2 is reserved for decimalsArray1.append (Random.randint (1, 10)) Else: Array1.append (Round (Random.uniform (1, 10), 2)) Array.append (array1)
Continue to increase complexity, the value of each row, from left to right values need to be increased in turn
ImportRandomarray= [] forJinchRange (0, 4): Array1= [] forIinchRange (0, 4): Radnum= Random.randint (0, 100) ifRadnum% 2 = = 0:#If an even number is generated between 1 and 10 integers, if the cardinality is generated by a decimal, and 2 is reserved for decimals ifLen (array1) = = 0:#determine whether the current list length is 0, if it is 0, the list does not have any elements, then add elements directlyelement = Random.randint (0, 100) Array1.append (Element)Else: Element= Random.randint (int (array1[-1]) + 1, int (array1[-1]) + 100)#Otherwise, the resulting element must be greater than the last element, and the boundary value of the last element and the generated random number will not be too small, and automatically growarray1.append (Element)Else: ifLen (array1) = =0:element= Round (random.uniform (0, 100), 2) Array1.append (Element)Else: Element= Round (Random.uniform (array1[-1] + 1, array1[-1] + 100), 2) Array1.append (Element) Array.append (array1)
Python face question