Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/284.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python3.x程序重复太多_Python_List_For Loop_Python 3.x - Fatal编程技术网

Python3.x程序重复太多

Python3.x程序重复太多,python,list,for-loop,python-3.x,Python,List,For Loop,Python 3.x,我正在写一个程序,打印字符串左栏的字母。这就是我所拥有的: str = '''Dear Sam: From Egypt we went to Italy, and then took a trip to Germany, Holland and England. We enjoyed it all but Rome and London most. In Berlin we met Mr. John O. Young of Messrs. Tackico & Co., on his wa

我正在写一个程序,打印字符串左栏的字母。这就是我所拥有的:

str = '''Dear Sam:
From Egypt we went to Italy, and then took a trip to Germany, Holland and England.
We enjoyed it all but Rome and London most.
In Berlin we met Mr. John O. Young of Messrs. Tackico & Co., on his way to Vienna.
His address there is 147 upper Zeiss Street, care of Dr. Quincy W. Long.
Friday the 18th, we join C. N. Dazet, Esquire and Mrs. Dazet, and leave at 6:30 A.M. for Paris
on the 'Q. X.' Express and early on the morning on the 25th of June start for home on <br>the S. S. King.
Very sincerely yours,
Signature of writer'''

splitstr = list(str)
list_a = []
list_b = []

for i in splitstr:
    if i == '\n':
        list_a.append(list_b)
        list_b = []
    else:
        list_b.append(i)

    for i in list_a:
       left_column = list_b[:1]
       print(left_column)
       break

或者它可以是垂直的,这无关紧要。

真的不需要这些,为什么不做一些类似的事情:

text = '''Dear Sam:
From Egypt we went to Italy, and then took a trip to Germany, Holland and England.
We enjoyed it all but Rome and London most.
In Berlin we met Mr. John O. Young of Messrs. Tackico & Co., on his way to Vienna.
His address there is 147 upper Zeiss Street, care of Dr. Quincy W. Long.
Friday the 18th, we join C. N. Dazet, Esquire and Mrs. Dazet, and leave at 6:30 A.M. for Paris
on the 'Q. X.' Express and early on the morning on the 25th of June start for home on the S. S. King.
Very sincerely yours,
Signature of writer
'''

print [line[0] for line in text.splitlines()]
[D',F',W',I',H',F',o',V',S']
另外,不要使用
str
作为变量名,因为这已经是内置函数的名称。

真的不需要这些,为什么不做如下操作:

text = '''Dear Sam:
From Egypt we went to Italy, and then took a trip to Germany, Holland and England.
We enjoyed it all but Rome and London most.
In Berlin we met Mr. John O. Young of Messrs. Tackico & Co., on his way to Vienna.
His address there is 147 upper Zeiss Street, care of Dr. Quincy W. Long.
Friday the 18th, we join C. N. Dazet, Esquire and Mrs. Dazet, and leave at 6:30 A.M. for Paris
on the 'Q. X.' Express and early on the morning on the 25th of June start for home on the S. S. King.
Very sincerely yours,
Signature of writer
'''

print [line[0] for line in text.splitlines()]
[D',F',W',I',H',F',o',V',S'] 另外,不要使用
str
作为变量名,因为这已经是内置函数的名称

['D', 'F', 'W', 'I', 'H', 'F', 'o', 'V', 'S']