python常用代码片段

字符串反转

my_string = 'ABCDE'
reversed_string=my_string[::-1]
print(reversed_string)

首字母大写

new_string = my_string.title()
  print(new_string) 

在字符串中查找唯一元素

 my_string = "aavvccccddddeee"

  # converting the string to a set
  temp_set = set(my_string)

  # stitching set into a string using join
  new_string = ''.join(temp_set)

  print(new_string)

列表推导式

 # Multiplying each element in a list by 2

  original_list = [1,2,3,4]

  new_list = [2*x for x in original_list]

  print(new_list)
  # [2,4,6,8]

变量交换

   a = 1
  b = 2

  a, b = b, a

  print(a) # 2
  print(b) # 1

将字符串拆分为子字符串列表

 string_1 = "My name is Chaitanya Baweja"
  string_2 = "sample/ string 2"

  # default separator ' '
  print(string_1.split())
  # ['My', 'name', 'is', 'Chaitanya', 'Baweja']

  # defining separator as '/'
  print(string_2.split('/'))
  # ['sample', ' string 2']

将字符串列表组合成单个字符串

list_of_strings = ['My', 'name', 'is', 'Chaitanya', 'Baweja']

  # Using join with the comma separator
  print(','.join(list_of_strings))

  # Output
  # My,name,is,Chaitanya,Baweja

检查回文字符串

my_string = "abcba"

  if my_string == my_string[::-1]:
      print("palindrome")
  else:
      print("not palindrome")

  # Output
  # palindrom

列表中元素统计

这样做有多种方法,但是我最喜欢的是使用 Python Counter 类。
Python 计数器跟踪容器中每个元素的频率, Counter()返回一个字典,其中元素作为键,频率作为值。
我们还使用 most_common()函数来获取列表中的 most_frequent 元素。

 # finding frequency of each element in a list
  from collections import Counter

  my_list = ['a','a','b','b','b','c','d','d','d','d','d']
  count = Counter(my_list) # defining a counter object

  print(count) # Of all elements
  # Counter({'d': 5, 'b': 3, 'a': 2, 'c': 1})

  print(count['b']) # of individual element
  # 3

  print(count.most_common(1)) # most frequent element
  # [('d', 5)]

使用枚举获取索引 / 值对

 my_list = ['a', 'b', 'c', 'd', 'e']

 for index, value in enumerate(my_list):
      print('{0}: {1}'.format(index, value))

  # 0: a
  # 1: b
  # 2: c
  # 3: d
  # 4: e

合并两个字典

 dict_1 = {'apple': 9, 'banana': 6}
 dict_2 = {'banana': 4, 'orange': 8}

  combined_dict = {**dict_1, **dict_2}

  print(combined_dict)
  # Output
  # {'apple': 9, 'banana': 4, 'orange': 8}

执行一短代码所需时间

  import time

  start_time = time.time()
  # Code to check follows
  a, b = 1,2
  c = a+ b
  # Code to check ends
  end_time = time.time()
  time_taken_in_micro = (end_time- start_time)*(10**6)

  print(" Time taken in micro_seconds: {0} ms").format(time_taken_in_micro)

展平列表清单

from iteration_utilities import deepflatten

  # if you only have one depth nested_list, use this
  def flatten(l):
    return [item for sublist in l for item in sublist]

  l = [[1,2,3],[3]]
  print(flatten(l))
  # [1, 2, 3, 3]

  # if you don't know how deep the list is nested
  l = [[1,2,3],[4,[5],[6,7]],[8,[9,[10]]]]

  print(list(deepflatten(l, depth=3)))
  # [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

从列表中随机取样

   import random

  my_list = ['a', 'b', 'c', 'd', 'e']
  num_samples = 2

  samples = random.sample(my_list,num_samples)
  print(samples)
  # [ 'a', 'e'] this will have any 2 random values


import secrets                            # imports secure module.
  secure_random = secrets.SystemRandom()    # creates a secure random object.

  my_list = ['a','b','c','d','e']
  num_samples = 2

  samples = secure_random.sample(my_list, num_samples)

  print(samples)
  # [ 'e', 'd'] this will have any 2 random values

数字化

以下代码段会将整数转换为数字列表。

num = 123456

  # using map
  list_of_digits = list(map(int, str(num)))

  print(list_of_digits)
  # [1, 2, 3, 4, 5, 6]

  # using list comprehension
  list_of_digits = [int(x) for x in str(num)]

  print(list_of_digits)
  # [1, 2, 3, 4, 5, 6]

检查唯一性

以下函数将检查列表中的所有元素是否唯一。

def unique(l):
      if len(l)==len(set(l)):
          print("All elements are unique")
      else:
          print("List has duplicates")

  unique([1,2,3,4])
  # All elements are unique

  unique([1,1,2,3])
  # List has duplicates
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值