python数据读入列表的几种常用方法

读入字符串每个字母

如:abcde

n=list(input())

输出:[‘a’, ‘b’, ‘c’, ‘d’, ‘e’]

 

 

读入一行多个字符串

如:abc  def  ghi

n=list(input().split())

输出:[‘abc’, ‘def’, ‘ghi’]

 

 

读入一行多个数字

如:123   456   789

n=list(map(int,input().split()))

输出:[123, 456, 789]

 

 

 

第一种(横向)

5

12 34 48 55 87

装进列表a:

n=int(input())

a=list(map(int,input().split()))

 

 

 

 

第二种(纵向)

5

12

34

48

55

87

装进列表a:

a=[]

n=int(input())

for  i  in  range(n):

    x=int(input())

    a.append(x)

 

 

 

 

第三种(输入数字,直到输入0为止)

12

34

43

36

89

0

 

装进列表a:

x=int(input())

a=[]

while x!=0:

    a.append(x)

    x=int(input())

 

 

 

 

第四种(输入字符串,直到输入“end”为止)

hhb

lzh

jqy

yyy

xcx

end

装进列表a:

x=input()

a=[]

while x!=“end”:

    a.append(x)

    x=input()