In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what are the high-frequency interview questions in Python". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "what are the high-frequency interview questions in Python"?
one。 Process the string "krig 1 | K1RO 2 | K2VOR 3 | K3RU 4" into a dictionary {kGRO 1th K1RO 2.
Str1 = "key 1 | K1VERV 2 | K2str1 | K3dict1 4" def str2dict (str1): dict1 = {} for iterms in str1.split ('|'): key,value = iterms.split (':') dict1 [key] = value return dict1# dictionary derivation d = {k:int (v) for t in str1.split ("|") for k, v in (t.split (":"))}
two。 Please sort by the age of the elements in alist from largest to smallest
Alist = [{'name':'a','age':20}, {' name':'b','age':30}, {'name':'c','age':25}] def sort_by_age (list1): return sorted (alist,key=lambda XRV x [' age'], reverse=True)
three。 What will be the output of the following code?
List = ['axiaxiajiajiajiaoyuzhuangyuanshou] print (list [10:])
The code will output [] without generating an IndexError error and, as expected, try to get the members of a list with more index than the number of members. For example, trying to get list [10] and subsequent members results in IndexError. However, trying to get a slice of the list, the initial index that exceeds the number of members does not generate an IndexError, but simply returns an empty list. This becomes a particularly disgusting problem because there are no errors at run time, making Bug difficult to track.
four。 Write a list generator to produce an isometric sequence with a tolerance of 11
Print ([Xero11 for x in range (10)])
five。 Given two lists, how do you find out the same elements and different elements?
List1 = [1Jing 2 Jing 3] list2 = [3 Jing 4 Jing 5] set1 = set (list1) set2 = set (list2) print (set1 & set2) print (set1 ^ set2)
six。 Would you please write a piece of python code to delete the duplicate elements in list?
L1 = list (set (L1)) print (L2)
Use the sort method of the list class:
L1 = list (set (L1)) l2.sort (key=l1.index) print (L2)
You can also write:
L1 = sorted (set (L1), key=l1.index) print (L2)
You can also use traversal:
L1 = ['baked if not i in if not i in L2: l2.append (I) print (L2)
seven。 Given two list A _ Magi B, please use to find out the same and different elements in A _ Magee B.
The same elements in print B: print (set (A) & set (B)) different elements in A Magi B: print (set (A) ^ set (B))
eight。 What is the difference between a new python class and a classic class?
a. In python, all classes that inherit object are new classes.
B. there are only new classes in Python3
C. It is the new class that inherits object in Python2, and the classical class that does not write the parent class.
d. At present, classical classes are basically not used in Python.
nine。 How many data structures are built into python?
a. Integer int, long integer long, floating point float, plural complex
b. String str, list list, meta-ancestor tuple
c. Dictionary dict, collection set
D. there is no long in Python3, only int with infinite precision
ten。 How does python implement singleton mode? Please write down two ways to implement it.
The first method: use the decorator
Def singleton (cls): instances = {} def wrapper (* args, * * kwargs): if cls not in instances: instances [cls] = cls (* args, * * kwargs) return instances [cls] return wrapper @ singletonclass Foo (object): passfoo1 = Foo () foo2 = Foo () print (foo1 is foo2) # True
The second method: use the base class
New is the real method to create an instance object, so override the new method of the base class to ensure that only one instance is generated when the object is created
Class Singleton (object): def _ new__ (cls, * args, * * kwargs): if not hasattr (cls,'_ instance'): cls._instance = super (Singleton, cls). _ _ new__ (cls, * args, * * kwargs) return cls._instance class Foo (Singleton): passfoo1 = Foo () foo2 = Foo (foo1 is foo2) # True
The third method: metaclass, metaclass is the class used to create class objects. Class objects must call the call method when creating instance objects, so make sure that only one instance is always created when calling call. Type is a metaclass of python.
Class Singleton (type): def _ call__ (cls, * args, * * kwargs): if not hasattr (cls,'_ instance'): cls._instance = super (Singleton, cls). _ call__ (* args, * * kwargs) return cls._instance# Python2class Foo (object): _ _ metaclass__ = Singleton# Python3class Foo (metaclass=Singleton): passfoo1 = Foo () foo2 = Foo () print (foo1 is foo2) # True so far I believe that you have a deeper understanding of "what are the high-frequency interview questions in Python?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.