Python crawler face questions 170: 2019 Edition [4]

List

31. how "1,2,3" to [ "1", "2", "3"]

split(",")

32. Given two list, A and B, and to identify the same elements and different elements

A, B of the same element: Print (SET (A) & SET (B)) 
A, B are different elements: Print (SET (A) ^ SET (B))

33. [[1,2], [3,4], [5,6]] line of code to expand the list, drawn [1,2,3,4,5,6]

a = [[1,2],[3,4],[5,6]]
b=[c for d in a for c in d]
print(b)

34. The combined list

a=[1,5,7,9]
b=[2,2,6,8]
c=a+b
print(c)

35. How to disrupt elements of a list?

import random
a = [1, 2, 3, 4, 5]
random.shuffle(a)
print(a)

Dictionary
36. Dictionary and pop operation del What is the difference

del be based on the value of the index (the location where the element) to delete, and did not return. The index value may be a pop pop up, and then the return value may be received.

37. The age of the dictionary ordering
? D1 = [
{ 'name': 'Alice', 'Age': 38 is},
{ 'name': 'Bob', 'Age':} 18 is,
'name': ' carl ',' Age ': 28},
]

sorted(d1, key=lambda x:x["age"])

38. Make the following two dictionaries combined a = { "A": 1, "B": 2}, b = { "C": 3, "D": 4}

a = {"A":1,"B":2}
b = {"C":3,"D":4}
c=dict(a,**b)
print(c)


39. How to use generative way to generate a dictionary, write a function code.

# Demand: The dictionary key and value exchange value; 
D = { ' A ' : ' . 1 ' , ' B ' : ' 2 ' } 

Print ({V: K for K, V in d.items ()})


40. How tuples ( "a", "b") and the tuple (1,2), the dictionary becomes { "a": 1, "b": 2}

a,b=("a","b"),(1,2)
c=dict(zip(a,b))
print(c)

 

Guess you like

Origin www.cnblogs.com/reseelei-despair/p/11325070.html