0
Follow
0
View

Discrete mathematics, partial order relation, number six can you give me some examples

liuguoyuch1406 注册会员
2023-02-28 17:20

For example, in programming, there is the following python list l

l=[
  {"a":1,"b":2}, 
  {"a":4,"b":1}, 
  {"a":2,"b":5}, 
  {"a":1,"b":4},
  {"a":1,"b":1},
  {"a":1,"b":3}
]

Sorts the list l , sorting only by the a field

l.sort(key=lambda item:item['a'])

Results are as follows:

[
  {'a': 1, 'b': 2}, 
  {'a': 1, 'b': 4}, 
  {'a': 1, 'b': 1}, 
  {'a': 1, 'b': 3}, 
  {'a': 2, 'b': 5}, 
  {'a': 4, 'b': 1}
]

You can see that a is sorted from smallest to largest, but when a is 1 , these elements are not sorted by b :

{'a': 1, 'b': 2}, 
{'a': 1, 'b': 4}, 
{'a': 1, 'b': 1}, 
{'a': 1, 'b': 3},

If we use multi-field sort

l.sort(key=lambda item:(item['a'],item['b']))

, the sorted result is sorted by the a field first, and then sorted by the b field if the a field is the same:

[
  {'a': 1, 'b': 1}, 
  {'a': 1, 'b': 2}, 
  {'a': 1, 'b': 3}, 
  {'a': 1, 'b': 4}, 
  {'a': 2, 'b': 5}, 
  {'a': 4, 'b': 1}
]

You can see that when a is all 1, it is sorted by element b from smallest to largest:

{'a': 1, 'b': 1}, 
{'a': 1, 'b': 2}, 
{'a': 1, 'b': 3}, 
{'a': 1, 'b': 4}

partial order plays an important role in programming. When comparing two elements in sorting, whether the comparison relationship between elements meets partial order will lead to different sorting results. More applications, in the database query, l =[ { "a" : 1 , "b" : 2 }, { "a" : 4 , "b" : 1 }, { "a" : 2 , "b" : 5 }, { "a" : 1 , "b" : 4 }, { "a" : 1 , "b" : 1 }, { "a" : 1 , "b" : 3 } ] 7 keyword, you can specify the database query according to 1 to more fields to do sort.

duwei26435753 注册会员
2023-02-28 17:20

Please provide some context so that you can understand your problem. What examples do you need?

About the Author

Question Info

Publish Time
2023-02-28 17:20
Update Time
2023-02-28 17:20

Related Question