python_16
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python_16相关的知识,希望对你有一定的参考价值。
字符串可使用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 = 0
i = 0
for v in lst:
length += 1
result = [None]*length
while i< length:
result[i] = lst[i]
i += 1
print("复制后的列表为:",result)
方式2:
a = ["a","b","c","d","e"]
arr_length=0
for i in a:
arr_length+=1
def iter_arr(n):
arr = []
i = 0
while i<=n-1:
arr+=[i]
i+=1
return arr
result = [""]*arr_length
for 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-8
class MyIter(object):
def __init__(self,n):
self.idx = 0
self.n = n
def __iter__(self):
return self
def __next__(self):
if self.idx <self.n:
val = self.idx
self.idx += 1
return val
else:
raise StopIteration()
myIter = MyIter(5)
print(next(myIter))
print(next(myIter))
for i in myIter:
print(i)
以上是关于python_16的主要内容,如果未能解决你的问题,请参考以下文章
Android 逆向使用 Python 解析 ELF 文件 ( Capstone 反汇编 ELF 文件中的机器码数据 | 创建反汇编解析器实例对象 | 设置汇编解析器显示细节 )(代码片段
ubuntu16.04 yum报错:There are no enabled repos. Run “yum repolist all“ to see the repos you have.(代码片段