day7 list job

  1. Create a list with 10 numbers in the list to ensure the order of elements in the list, arrange the weight of the list, and sort the list in descending order
For example:[70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
		--- After weight removal [70, 88, 91, 107, 234, 177, 282, 197]
  	---- Descending sort [282, 234, 197, 177, 107, 91, 88, 70]
nums=[70, 88, 91, 70, 107, 234, 91, 177, 282, 197]
index=0
while index<len(nums):
    if nums[index] in nums[index+1:]:
        del nums[index]
    else:
        index+=1
nums .sort (reverse=True)
print(nums)
#[282, 234, 197, 177, 107, 91, 88, 70]

  1. Using the list derivation, complete the following requirements

a. Generate a data list with 3 digits in 1-100

The result is [3, 13, 23, 33, 43, 53, 63, 73, 83, 93]
list=[x for x in range(1,101) if x%10==3]
print(list)
#[3, 13, 23, 33, 43, 53, 63, 73, 83, 93]

b. The integer in the list is extracted by list derivation

For example:[True, 17, "hello", "bye", 98, 34, 21] --- [17, 98, 34, 21]
nums1=[True, 17, "hello", "bye", 98, 34, 21]
nums2=[17, 98, 34, 21]
nums=nums1+nums2
list=[x for x in nums if  type(x)==int ]
print(list)
#[17, 98, 34, 21, 17, 98, 34, 21]

c. Use the list derivation to store the length of the string in the specified list

For example: ["good", "nice", "see you", "bye"] --- [4, 4, 7, 3]
nums=["good", "nice", "see you", "bye"]
result=[len(x) for x in nums if type(x)==str]
print(result)

d. Use list derivation to delete elements with integer single digits less than 5 in the list

For example:[24, 'abc', 99, True, 21, 38, 'hello'] --- ['abc', 99, True, 38, 'hello']
nums=[24, 'abc', 99, True, 21, 38, 'hello']
result = [x for x in nums if  not(type(x) == int and x % 10 < 5)]
print(result)

e. The element obtained by list derivation is the last element of each tuple in the list of tuples

For example:[(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)]  --- [30, 'hello', 3.4, False]
nums=[(10, 20, 30), ('abc', 'hello'), (1, 2, 3.4), (True, False)]
list=[ x[-1]for x in nums if type(x)==tuple]
print(list)

f. Use the list derivation to multiply all odd numbers in the number list by 2 and divide all even numbers by 2

for example: [23, 4, 67, 88, 90, 21]  -> [46, 2, 134, 44, 45, 42]
nums=[23, 4, 67, 88, 90, 21]
result=[x*2 if x%2!=0 else int(x/2) for x in nums]
print(result)
  1. Known as a list, get all the subscripts of the specified element in the list

    For example:[10, 20, 34, 10, 9, 78]
    10 Subscript of:[0, 3]
    20 Subscript of:[1]
    30 Subscript of:[]
    
    nums=[10, 20, 34, 10, 9, 78]
    result1=[x for x in range(len(nums)) if nums[x]==10]
    print(result1)
    result2=[nums.index(20)]
    print(result2)
    
  2. *Given a list of numbers, the writer determines that the list is a continuously increasing list.

    For example:
    [1, 2, 3, 4, 5]   -> True
    [23, 45, 78, 90]  -> True
    [1, 3, 2, 4, 5]	-> False
    
  3. Given two lists, cross merge the two lists according to the following rules

    A = [10, 20, 30, 40, 50]
    B = [100, 200, 300]
    result:[10, 100, 20, 200, 30, 300, 40, 50]
    
  4. Two sequential tables are known. Merge the two lists, and the elements in the merged new list are still incremental lists

    A = [10, 20, 30, 40, 50]
    B = [25, 44, 60]
    result:[10, 20, 25, 30, 40, 45, 50, 60]
    

Day 7 list and list derivation

1, List dependent operator

1. Mathematical operators: +*(

1) '+': List 1 + list 2 - merge the elements of two lists to produce a new list

2) ': list N/N * list - the elements in the list are repeated N times to produce a new list

nums=[1,2,2,3,4]
new_nums=nums*3
print(new_nums)
#[1, 2, 2, 3, 4, 1, 2, 2, 3, 4, 1, 2, 2, 3, 4]
2. Comparison operator

Different types of lists can use = = or= To compare equality, but you cannot use <, >, < =, > = to compare size

1)==,!= (the list is orderly, and the order will affect whether it is equal)

print([10,20,30])==[10,20,30]
print([10,20,30])!=[10,30,20]

2) <, >, < =, > = - compare the size of each element in order

2, List correlation function

1. max

max (sequence) - gets the largest element in the sequence

Min (sequence) - gets the smallest element in the sequence

2. sum

Sum (digital sequence) - sum all elements in the sequence

3. sorted

1) sorted - sorts the elements in the sequence from small to large, and then produces a new list

2) Sorted (sequence, reverse=True) - sort the elements in the sequence from large to small to produce a new list

4. len

Len (sequence) - gets the number of elements in the sequence

5. list

List - creates a new list with the elements in the sequence as the elements of the list

3, List related methods

1. List. clear() - clear the list
2. list as like as two peas (.Copy) - copy the original list to produce a new identical list.

List [:], list * 1, list + [] - are all shallow copies

Note: when a variable saves data, it actually saves the address of the data in memory. When a variable directly assigns a value to another variable, it gets the same address. At this time, operating on one of the lists will affect the other list. In the case of copying, the list will get a memory address again, and the new list will not be affected by the original list operation.

3. List. Count (element) - counts the number of specified elements in the list
4. List. Extend (sequence) - adds all the elements in the sequence to the list

Note the difference between append and append: append adds the whole sequence to the list, and extend takes out all elements and adds them to the list

5. List. index (element) - get the subscript value of the element in the list

When there are multiple identical elements in the list, the first one will be printed. If the element is no longer in the list, an error will be printed

6. List. reverse () - invert the elements in the original list position
7. Sorting

1) List. sort() / list. sort(reverse=True) - sort in the original list

2) Sorted (sequence) / sorted (sequence, reverse=true) - create a new list to sort

4, List derivation

1. Derivation structure 1

[expression for variable in sequence] - let the variable take values one by one in the sequence, and take the value of the expression as an element in the list every time a value is taken

2. Push down structure 2

[expression for variable in sequence if conditional statement] - create a list. The variable takes values from the sequence, one by one. Until it is taken, judge whether the conditional statement is true every time a value is taken. If it is true, the result of the calculation expression is the element of the list.

5, Tuples and ternary operators

1. Tuples are immutable lists

Query, in and not in, mathematical operation, comparison operation and related function tuple are supported

Syntax: (element 1, element 2, element 3 ···)

Tuples are immutable and ordered

2. Kong Yuanzu - > ()
3. Tuple with one element - > (element,)
4. Three eye operation algorithm

Syntax: value 1 if expression else value 2 - if the value of the expression is true, the result is value 1, otherwise the result is value 2

Keywords: Python

Added by ecco on Wed, 08 Dec 2021 01:14:57 +0200