zoukankan      html  css  js  c++  java
  • 案例:手动输入一个字符串,打散放进一个列表,小写字母反序 大写字母保持不变

    思路:

    1. 将手动传入的字符串用list函数将其打散成为一个列表1
    2. 循环列表1,判断每个元素字母的大小写
    3. 小写:将其追加到一个新列表2中
    4. 大写:记录下其索引及元素值到一个字典中
    5. 循环完成后,将新列表中所有的小写元素反转
    6. 循环遍历字典,将键值对应的添加到列表2中
    7. 循环完成后,输出列表2

    代码实现

     1 string = input('请输入一个字符串:')
     2 # 将输入的字符串打散到lists列表中
     3 lists = list(string)
     4 # 创建一个新列表用来存放变换后的数据
     5 new_lists = []
     6 # 创建一个字典,用来存放大写字母的索引及其对应的值
     7 dictionary = {}
     8 i = 0
     9 while i < len(lists):
    10     if 'a' < lists[i] < 'z':
    11         # 小写,按照顺序存放到new_lists列表中
    12         new_lists.append(lists[i])
    13     else:
    14         # 大写,将索引及其对应的值存放在dictionary
    15         dictionary[i] = lists[i]
    16     i += 1
    17 
    18 # 反转列表(小写字母元素)
    19 new_lists.reverse()
    20 # 循环遍历字典,将键值对应的添加到new_lists列表中
    21 for key in dictionary:
    22     new_lists.insert(key, dictionary[key])
    23 
    24 print(new_lists)
    View Code
  • 相关阅读:
    linux command lynx
    git bisect
    git blame (10)
    git crate&query&delete tag(九)
    git crate patch and check&apply patch(八)
    learning shell check host dependent pkg (8)
    【泛型】
    sql server 查询所有表的记录数
    【转】EXCEL中如何实现由总表自动得出分表
    MS Chart 打印
  • 原文地址:https://www.cnblogs.com/SakuraYuanYuan/p/10711408.html
Copyright © 2011-2022 走看看