当前位置:   article > 正文

Python ------技巧系列一_print(",".join(list(map(str, range(n)))))

print(",".join(list(map(str, range(n)))))

1. 交换两个变量的值

  1. a, b = 5, 10
  2. print(a, b)
  3. a, b = b, a
  4. print(a, b)

2. 将列表中所有元素拼接成字符串

  1. a = ["Python", "is", "Good"]
  2. print(" ".join(a))
  3. numbers = [2, 3, 5, 10]
  4. print(",".join(map(str, numbers)))
  5. data = [2, 'hello', 3, 3.14]
  6. print(','.join(map(str, data)))

3.  查找列表中出现次数最多的值

  1. a = [1, 2, 3, 1, 2, 3, 2, 2, 4, 5, 1]
  2. print(max(set(a), key=a.count))
  3. from collections import Counter
  4. cnt = Counter(a)
  5. print(cnt.most_common(3))
  6. # 2出现4 次 , 1 出现 3 次
  7. # [(2, 4), (1, 3), (3, 2)]

4. 字符串反转

  1. a = "abcdefghijklmnopqrstuvwxyz"
  2. print(a[::-1])
  3. for char in reversed(a):
  4. print(char)
  5. num = 123456789
  6. print(int(str(num)[::-1]))

5. 反转列表

  1. a = [5, 4, 3, 2, 1]
  2. print(a[::-1])
  3. for item in reversed(a):
  4. print(item)

6. 转换二维数组

  1. original = [['a', 'b'], ['c', 'd'], ['e', 'f']]
  2. transposed = zip(*original)
  3. print(list(transposed))

7. 链式函数调用

  1. def multiplication(a, b):
  2. return a * b
  3. def add(a, b):
  4. return a + b
  5. b = False
  6. print((multiplication if b else add)(5, 7))

8. 拷贝

  1. a = [1, 2, 3, 4, 5]
  2. b = a
  3. b[0] = 10
  4. print(a)
  5. print(b)
  6. a = [1, 2, 3, 4, 5]
  7. b = a[:]
  8. b[0] = 10
  9. print(a)
  10. print(b)
  11. a = [1, 2, 3, 4, 5]
  12. print(list(a))
  13. print(a.copy())
  14. from copy import deepcopy
  15. d = [[1, 2], [3, 4]]
  16. d2 = deepcopy(d)
  17. print(d2)

9. 根据值进行字典排序

  1. d = {'apple': 10, 'orange': 20, 'banana': 5, 'rotten tomato': 1}
  2. print(sorted(d.items(), key=lambda x: x[1]))
  3. from operator import itemgetter
  4. print(sorted(d.items(), key=itemgetter(1)))
  5. print(sorted(d, key=d.get))

10. 合并字典

  1. d1 = {'a': 1}
  2. d2 = {'b': 2}
  3. # python 3.5 才支持
  4. print({**d1, **d2})
  5. print(dict(d1.items() | d2.items()))
  6. d1.update(d2)
  7. print(d1)

11. 获取列表中的最小值和最大值的索引

  1. lst = [40, 10, 20, 30]
  2. def minIndex(lst):
  3. return min(range(len(lst)), key=lst.__getitem__)
  4. def maxIndex(lst):
  5. return max(range(len(lst)), key=lst.__getitem__)
  6. print(minIndex(lst))
  7. print(maxIndex(lst))
  8. # 最小值和最大值
  9. print(min(lst))
  10. print(max(lst))

12. 从列表中删除重复项

  1. items = [2, 2, 3, 3, 1]
  2. newitems = list(set(items))
  3. print(newitems)
  4. from collections import OrderedDict
  5. items = ['foo', 'bar', 'bar', 'foo']
  6. print(list(OrderedDict.fromkeys(items).keys()))

13. 将列表进行全排列组合

  1. import itertools
  2. horses = [1, 2, 3, 4]
  3. print (list(itertools.permutations(horses)))
  4. [(1, 2, 3, 4), (1, 2, 4, 3), (1, 3, 2, 4), (1, 3, 4, 2), (1, 4, 2, 3), (1, 4, 3, 2), (2, 1, 3, 4), (2, 1, 4, 3), (2, 3, 1, 4), (2, 3, 4, 1), (2, 4, 1, 3), (2, 4, 3, 1), (3, 1, 2, 4), (3, 1, 4, 2), (3, 2, 1, 4), (3, 2, 4, 1), (3, 4, 1, 2), (3, 4, 2, 1), (4, 1, 2, 3), (4, 1, 3, 2), (4, 2, 1, 3), (4, 2, 3, 1), (4, 3, 1, 2), (4, 3, 2, 1)]

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/208025
推荐阅读
相关标签
  

闽ICP备14008679号