leetcode 中的list处理

list.extend()

list1.extend(list2(or string)) 将list2(or string)的所有元素添加到list1中;

list1.append(list2(or string)) 将list2(or string)作为一个元素添加到list1中;

注意dtype( list1.extend() ) = Nonetype 所以无法对其进行列表操作(.sort()之类的)  ->append也一样

 1  1 class Solution:
 2  2     def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
 3  3         nums1.extend(nums2)
 4  4         nums1.sort()
 5  5         x=len(nums1)
 6  6         if (x%2)==1:
 7  7             i=int((x-1)/2)
 8  8             return float(nums1[i])
 9  9         else:
10 10             i=int(x/2)
11 11             j=int((x/2)-1)
12 12             return (nums1[i]+nums1[j])/2.0
median of 2 list

猜你喜欢

转载自www.cnblogs.com/lizhiqing/p/10611083.html