博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
python_16
阅读量:7243 次
发布时间:2019-06-29

本文共 2750 字,大约阅读时间需要 9 分钟。

字符串可使用sorted()函数

s = "huhongiang"

sorted(s)
['a', 'g', 'g', 'h', 'h', 'i', 'n', 'n', 'o', 'u']
l = sorted(s)
l
['a', 'g', 'g', 'h', 'h', 'i', 'n', 'n', 'o', 'u']

习题1:"abcdefgh"里面挑出3个字母进行组合,一共有多少种组合,要求三个字母中不能有任何重复的字母,三个字母的同时出现次数,在所有组合中只能出现一次,例如出现abc了,不能出现cab和bca等。

方式1:

s = "abcdefgh"result = []temp_list = []for k in s:    for v in s:        for j in s:            if k != v and k != j and v !=j:                #sorted("abc") ==》["a","b","c"]                #[sorted(s) for s in result]每一个元素是一个包含三个字母的列表                #if sorted(list(k+v+j))  not in [sorted(list(s)) for s in result]                if sorted(list(k+v+j)) not in [sorted(s) for s in result]:                    result.append(k+v+j)                    #print(result)print("组合数:",len(result))

方式2:

s = "abcdefgh"result = []temp_list = []for k in s:    for v in s:        for j in s:            if k != v and k != j and v !=j:                #join后直接比较字符串                if "".join(sorted(list(k+v+j)))  not in ["".join(sorted(list(s))) for s in result]:                    result.append(k+v+j)                    #print(result)print("组合数:",len(result))

习题2:复制一个列表,不能使用切片和复制的函数进行赋值,尽可能使用少的内置函数

方式1:

lst = ["a","b","c","d","e"]length = 0i = 0for v in lst:    length += 1result = [None]*lengthwhile i< length:    result[i] = lst[i]    i += 1print("复制后的列表为:",result)

方式2:

a = ["a","b","c","d","e"]arr_length=0for i in a:    arr_length+=1def iter_arr(n):    arr = []    i = 0    while i<=n-1:        arr+=[i]        i+=1    return arrresult = [""]*arr_lengthfor i in iter_arr(arr_length):    result[i] = a[i]print(result)

枚举:enumerate

可以遍历元素对应的行号 和元素
lst_1 = [1,2,3,["a","b"]]
for idx,v in enumerate(lst_1):
print(idx,v)
迭代器

可迭代列表、元组、字符串及字典,字典只迭代key

可使用next() next()进行输出元素

使用next()输出元素

列表

l = [5,6,7]

it = iter(l)

it.next()

5
it.next()
6
it.next()
7

没有元素时候迭代器会报错

it.next()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration

字典:

it = iter({1:2,3:4})

it.next()
1
it.next()
3
it.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration

it =iter({1:2,3:4}.values())

it.next()
2
it.next()
4
it.next()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration

使用next()输出元素

l = [5,6,7]

it = iter(l)
next(it)
5
next(it)
6
next(it)
7
next(it)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
StopIteration

遍历迭代器

l = [1,2,3,4,6]

it = iter(l)
for i in it:
... print(i)
...
1
2
3
4
6
自定义迭代器
iter()和next()方法
这两个方法是迭代器最基本的方法:
➢ 一个用来获得迭代器对象
➢ 一个用来获取容器中的下一个元素

#encoding=utf-8class MyIter(object):    def __init__(self,n):        self.idx = 0        self.n = n    def __iter__(self):        return self    def __next__(self):        if self.idx 

转载于:https://blog.51cto.com/13496943/2298843

你可能感兴趣的文章
CODEVS 1047 邮票面值设计
查看>>
谈工程师的价值和发展
查看>>
1、查找字符位置函数
查看>>
去掉标题栏/ActionBar后点击menu键时应用崩溃
查看>>
求1+2+3+...+n
查看>>
bzoj 1854: [Scoi2010]游戏
查看>>
bzoj 1432: [ZJOI2009]Function
查看>>
HashMap泛型使用
查看>>
MTK Sensor越界导致的系统重启问题分析报告
查看>>
Swift-JSON Encode && Decode
查看>>
(效果五)js获取客户端ip地址及浏览器信息
查看>>
XAML中的空格、换行、Tab
查看>>
Python解析XML
查看>>
第43条:返回零长度的数组或者集合,而不是null
查看>>
初识numpy的多维数组对象ndarray
查看>>
java代码随机数100个,10个一输出显示======
查看>>
初始化代码块
查看>>
[NOI2008]志愿者招募
查看>>
Linux——网络端口的状态netstat、ifconfig
查看>>
canvas元素简易教程(5)(大部分转自火狐,自己只写了简单的代码分析)
查看>>