您的位置:首页 > 编程语言 > Python开发

一段代码学习几个Python知识点

2016-03-19 11:09 941 查看
###########################################################

zip(a, b)函数,将它可迭代的对象a, b作为参数,将a, b中对应的元素打包成一个个tuple(元组),然后返回由tuple组成的列表。

比如:

def show_table(table):
test = zip(*table)
print (test)

show_table([['A','B'],['CCC','DDD']])


输出为:

[('A', 'CCC'), ('B', 'DDD')]

###########################################################

enumerate() 可以同时取出可迭代对象的index 和 content。

比如:

myList = [10, 4, 6]

for i, val in enumerate(myList):
print ("index: " + str(i) + " value: " + str(val))


输出为:

index: 0 value: 10

index: 1 value: 4

index: 2 value: 6

###########################################################

format()函数用来替代%控制输出结果的显示。format()高端之处在于可以以变量指定输出显示的位数,比如%3f中的3。

具体用法如下:



举例:

print ("{}".format(5))
print ("{}{}".format(5, "test"))
print ("{0}{0}{1}".format(5, "test"))
print ("{value}{value}{text}".format(value = 5, text = "test"))
myList = [5, "test"]
print ("{0[0]}{0[0]}{0[1]}".format(myList))

#":number" specify the format
print ("{:2}{:10}".format(5, "test"))
# < means left-aligned, > means right-aligned, ^ means center-aligned
print ("{:<2}{:<10}".format(5, "test"))
print ("{:<2}{:>10}".format(5, "test"))
print ("{:>2}{:<10}".format(5, "test"))
print ("{:>2}{:>10}".format(5, "test"))
# a, b here is the fill character
print ("{:a>2}{:b>10}".format(5, "test"))
#specify the format using variable
print ("{:a>{}}{:b>10}".format(5, 2, "test"))


输出结果:
5

5test

55test

55test

55test

5test

5 test

5 test

5test

5 test

a5bbbbbbtest

a5bbbbbbtest

###########################################################

List Python comprehensive 看起来很怪异,其实使用很方便简单

比如:

noprimes = [j for i in range(2, 8) for j in range(i*2, 50, i)]
print (noprimes)

noprimes = []
for i in range(2, 8):
for j in range(i*2, 50, i):
noprimes.append(j)
print (noprimes)


输出为:

[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 10, 15, 20, 25, 30, 35, 40, 45, 12, 18, 24, 30, 36, 42, 48, 14,
21, 28, 35, 42, 49]

[4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 10, 15, 20, 25, 30, 35, 40, 45, 12, 18, 24, 30, 36, 42, 48, 14, 21,
28, 35, 42, 49]

words = 'The quick brown fox jumps over the lazy dog'.split()
print words

stuff = [[w.upper(), w.lower(), len(w)] for w in words]
for i in stuff:
print i

stuff = []
for w in words:
stuff.append([w.upper(), w.lower(), len(w)])
for i in stuff:
print i


输出为:

['The', 'quick', 'brown', 'fox', 'jumps', 'over', 'the', 'lazy', 'dog']

['THE', 'the', 3]

['QUICK', 'quick', 5]

['BROWN', 'brown', 5]

['FOX', 'fox', 3]

['JUMPS', 'jumps', 5]

['OVER', 'over', 4]

['THE', 'the', 3]

['LAZY', 'lazy', 4]

['DOG', 'dog', 3]

['THE', 'the', 3]

['QUICK', 'quick', 5]

['BROWN', 'brown', 5]

['FOX', 'fox', 3]

['JUMPS', 'jumps', 5]

['OVER', 'over', 4]

['THE', 'the', 3]

['LAZY', 'lazy', 4]

['DOG', 'dog', 3]

Dict Python Comprehensive

比如:

print {i : chr(65+i) for i in range(4)}

list_of_email_addrs = ["barry@zope.com", "barry@python.org", "guido@python.org"]
print {x.lower() : 1 for x in list_of_email_addrs}


输出为:

{0: 'A', 1: 'B', 2: 'C', 3: 'D'}

{'barry@python.org': 1, 'guido@python.org': 1, 'barry@zope.com': 1}

###########################################################

实例,以表格形式输出list of list 中的元素,要求元素之间以“|”隔开,且每列以最长的元素为列宽

def show_table(table):

col_width = [max(len(x) for x in col) for col in zip(*table)]

for index, line in enumerate(table):
table_row = ""
for i, x in enumerate(line):
table_row += "{:{}}".format(x, col_width[i]) + " | "

print "| " + table_row

def main():

show_table([['10','2','300'],['4000','50','60'],['7','800','90000']])
show_table([['A','B'],['CCC','DDD']])

if __name__ == '__main__':
main()
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: