Python 基础第二话:关键字、容器小练习

目录

练习 01:使用循环和列表推导器找出单词长度大于某个数字的单词    

练习 02:寻找两个列表中的相同元素   

练习 03:去除一个列表中相邻且重复的元素 

练习 04:用户名密码对应

练习 05:使用列表推导式,打印出颜色与尺寸的(组合)

练习 06:词频统计

练习 07:实现行列互转

练习 08:实现求指定长度的 Fibonacci 数列

练习 09:[]对应检查


练习 01:使用循环和列表推导器找出单词长度大于某个数字的单词
    

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
练习 01:使用循环和列表推导器找出单词长度大于某个数字的单词。
    words = ["apple","banana","orange","peack","kiwi"]
"""
words = ["apple","banana","orange","peack","kiwi"]
limit_n = 5

print("##### while 循环 #####")
n = 0
while n <= (len(words) - 1):
    if len(words[n]) > limit_n:
        print(words[n])
    n += 1

print("##### for 循环 #####")
for word in words:
    if len(word) > limit_n:
        print(word)

print("##### 列表推导器 #####")
limit_words = [word for word in words if len(word) > limit_n]
print(limit_words)

练习 02:寻找两个列表中的相同元素
   

"""
练习 02:寻找两个列表中的相同元素。
    list01 = [1,2,5]
    list02 = [6,2,7,9]
"""
list01 = [1,2,5]
list02 = [6,2,7,9]

for n in list01:
    if n in list02:
        print(n)

练习 03:去除一个列表中相邻且重复的元素
 

"""
练习 03:去除一个列表中相邻且重复的元素。
    list01 = [1,2,3,4,4,4,4,5,6,6,6,6,6,6,8,8,8,8,8,8,12,12,12,12,12,13]
"""
list03 = [1,2,3,4,4,4,4,5,6,6,6,6,6,6,8,8,8,8,8,8,12,12,12,12,12,13]
list_new03 = [x for x in set(list03)]
print(list_new03)

  

练习 04:用户名密码对应

"""
练习 04:用户名密码对应,给定两个列表,一个存放用户名,一个存放密码。请将用户名和密码按顺序进行对应为一个元素。
    username = ['jack','bob','john']
    password = ['123','859','hello']
"""
username = ['jack','bob','john']
password = ['123','859','hello']
user_pwd = [i for i in zip(username,password)]
print(user_pwd)
print(user_pwd[1])

   

练习 05:使用列表推导式,打印出颜色与尺寸的(组合)

"""
练习 05:使用列表推导式,打印出颜色与尺寸的(组合)
    colors = ['black','white']
    sizes = ['S','M','L']
"""
colors = ['black','white']
sizes = ['S','M','L']
style_all = [(x, y) for x in colors for y in sizes]
print(style_all)

 

练习 06:词频统计
 

"""
练习 06:词频统计
    list01 = ['sklearn','AI','julydu.com','Caffe','AI','sklearn']
    对 list01 包含的单词,利用 dict 统计词频
    对每个参数进行判断,若在则对应的 value+1
    否则根据该字符创建一个 key 并且 value 设置为 1
    最后输出该词典
"""
list01 = ['sklearn','AI','julydu.com','Caffe','AI','sklearn','AI']
word_dict = dict()
for list_k in list01:
    if list_k in word_dict:
        word_dict[list_k] += 1
    else:
        word_dict.setdefault(list_k, 1)
print(word_dict)

练习 07:实现行列互转
 

"""
练习 07:实现行列互转
    arr = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
"""
arr01 = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
arr02 = [x for x in zip(*arr01)]
print(arr02)

arr03 = [[r[col] for r in arr01] for col in range(len(arr01[0]))]
print(arr03)

练习 08:实现求指定长度的 Fibonacci 数列
 

"""
练习 08:实现求指定长度的 Fibonacci 数列(任何一个位置的数字就等于它前两个数字的和)
    Fib 数组初始为[0,1]
    要求分别使用循环和列表实现
"""
fib = [1,1]
n = 1
while n <= 13:
    fib += [fib[n] + fib[n-1]]
    n += 1
print(fib)

练习 09:[]对应检查
 

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
练习 09:[]对应检查
    输入含有‘[]’的字符串,输出对中括号出现规则的检测结果,举个例子:
    []          OK      ][          NOT OK
    [][]        OK      ][][        NOT OK
    [[][]]      OK      []][[]      NOT OK
    [[][[]]]    OK      ][]][[][    NOT OK
"""
brackets = input("请输入含有‘[]’的字符串:")
bracket_dict = {'L': 0, 'R': 0}
for bracket in brackets.replace('[','L').replace(']','R'):
    bracket_dict[bracket] += 1
    if bracket_dict['L'] < bracket_dict['R']:
        break
if bracket_dict['L'] == bracket_dict['R']:
    print(brackets, "is OK!")
else:
    print(brackets, "is NOT OK!")

 

你可能感兴趣的:(Python)