Python:遍历字符串和列表
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python:遍历字符串和列表相关的知识,希望对你有一定的参考价值。
## The first example uses a string as the sequence to create a list of characters in the string: >>>word = "Python" >>>list = [] >>>for ch in word: >>> list.append(ch) >>>print list ['P', 'y', 't', 'h', 'o', 'n'] ## This example uses the range() function to create a temporary sequence of integers the size of a list so the items in the list can be added to a string in order: >>>string = "" >>>for i in range(len(list)): >>> string += list[i] >>>print string Python ## This example uses the enumerate(string) function to create a temporary sequence. The enumerate function returns the enumeration in the form of (0, s[0]), (1, s[1]), and so on, until the end of the sequence string, so the for loop can assign both the i and ch value for each iteration to create a dictionary: >>>dict = {} >>>for i,ch in enumerate(string): >>> dict[i] = ch >>>print dict {0: 'P', 1: 'y', 2: 't', 3: 'h', 4: 'o', 5: 'n'} ## This example uses a dictionary as the sequence to display the dictionary contents: >>>for key in dict: >>> print key, '=', dict[key] 0 = P 1 = y 2 = t 3 = h 4 = o 5 = n
以上是关于Python:遍历字符串和列表的主要内容,如果未能解决你的问题,请参考以下文章