通常涉及到去重操作最好使用set,但是考虑到某些时候可能遇到不允许使用set的情况,那就自己实现一下: l = [2, 4, 5, 6, 1, 3, 4, 5] def f(l, b=0, c=1): a = len(l) if a > 1: l.sort() if l[b] == l[c]: l.pop(b) a = len(l) # 删除一个元素之后,列表的长度变了,同时,下标b和c对应的值也变了,所以重新对a赋值,而b和c不变 else: b, c = c, c+1 if a > c:
方法1:使用set函数 s=set(list),然后再list(s) 方法2:append def delList(L): L1 = [] for i in L: if i not in L1: L1.append(i) return L1 print(delList([1,2,2,3,3,4,5])) print(delList([1,8,8,3,9,3,3,3,3,3,6,3])) 方法3:count,remove def delList(L): for i in L: if
现在有一个长度20的SET,其中每个对象的内容是随机生成的字符串,请写出遍历删除LIST里面字符串含"2"的对象的代码. public class RemoveTwo { //length用户要求产生字符串的长度 public static String getRS(int length){ String str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; Random rand
题目如下: 子线程循环10次,接着主线程循环100,接着又回到子线程循环10次, 接着再回到主线程又循环100,如此循环50次 思路如下: 子线程语主线程为互斥,可用SYNCHRONIZED.很容易想到如下代码 package concurrent; public class theFirstIdea{ /** * @param args */ public static void main(String[] args) { new Thread(//子线程 new Runnable(){ pu
要求说明: 输入三个整数x,y,z,请把这三个数由小到大输出. 实现代码: 第1种方法: import java.util.Scanner; public class xyzMaxMin{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入3个整数:"); int x = sc.nextInt(); int y = sc.nextInt