打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
Python 1000 道练习题(6)

1、找出数列中是否存在相同数字

def test_distinct(data):
  if len(data) == len(set(data)):
    return True
  else:
    return False;
print(test_distinct([1,5,7,9]))
print(test_distinct([2,4,5,5,7,9]))

输出

True
False

2、从数字列表中删除并打印第三个数字,直到列表变空

def remove_nums(int_list):
  #列表下标从 0 开始
  position = 3 - 1 
  idx = 0
  len_list = (len(int_list))
  while len_list>0:
    idx = (position+idx)%len_list
    print(int_list.pop(idx))
    len_list -= 1
    
nums = [10,20,30,40,50,60,70,80,90]
remove_nums(nums)

输出

30
60
90
40
80
50
20
70
10

3、将字符串转换为列表并输出所有单词及其频率

string_words = '''Python is a programming language that lets you work quickly
and integrate systems more effectively.'''


word_list = string_words.split()

word_freq = [word_list.count(n) for n in word_list]

print("字符串:\n {} \n".format(string_words))
print("列表:\n {} \n".format(str(word_list)))
print("各单词出现的频率是:\n {}".format(str(list(zip(word_list, word_freq)))))

输出

字符串:
 Python is a programming language that lets you work quickly
and integrate systems more effectively.

列表:
 ['Python''is''a''programming''language''that''lets''you''work''quickly''and''integrate''systems''more''effectively.']

各单词出现的频率是:
 [('Python'1), ('is'1), ('a'1), ('programming'1), ('language'1), ('that'1), ('lets'1), ('you'1), ('work'1), ('quickly'1), ('and'1), ('integrate'1), ('systems'1), ('more'1),
('effectively.'1)]

4、输出本地计算机操作系统的相关信息

import platform as pl

os_profile = [
    'architecture',
    'machine',
    'node',
    'platform',
    'processor',
    'python_version',
    'system',
    'version',
]
for key in os_profile:
    if hasattr(pl, key):
        print(key + " : " + str(getattr(pl, key)()))

输出

architecture : ('64bit''WindowsPE')
machine : AMD64
node : Donsole
platform : Windows-7-6.1.7601-SP1
processor : Intel64 Family 6 Model 94 Stepping 3, GenuineIntel
python_version : 3.8.3
system : Windows
version : 6.1.7601

5、输出给定数字的所有排列可能

def permute(nums):
  result_perms = [[]]
  for n in nums:
    new_perms = []
    for perm in result_perms:
      for i in range(len(perm)+1):
        new_perms.append(perm[:i] + [n] + perm[i:])
        result_perms = new_perms
  return result_perms

my_nums = [1,2,3]
print("给定列表数字的排列可能有:\n",permute(my_nums))

输出

给定列表数字的排列可能有:
 [[321], [231], [213], [312], [132], [123]]


今天又和大家一起学习了 10 道 Python 练习题;大家都学会了吗?

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
盘点一个Python列表(元素多样)处理的实战题目(使用正则表达式也可以实现)
Python 动态规划算法,计算单词距离
7个超实用的Python小技巧!
python解决排列组合问题的4种方式
Python: 告别Print?优秀的Debug神器---pysnooper
惯用Python的5个技巧(循环)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服