2012-09-26 8 views
0

내가 다시 새 행에 인쇄 목록의 각 요소의 앞에 문자열을 인쇄 할 전에 반복과 다른 행의 각 목록 요소 :인쇄 문자열을 각 요소 파이썬

예 :

test = ["aaa", "bee", "cee"] 
    print("hello, %s" % "\n".join(storageVolume)) 

내가이에서 얻을 것은 :

hello, aaa 
    bee 
    cee 

내가 원하는 것은 :

hello, aaa 
    hello, bee 
    hello, cee 

도움을 주시면 감사하겠습니다.

+3

과'storageVolume'은에서 온다 ...? –

답변

3
for x in test: 
    print "Hello, {0}".format(x) 
+2

이것은 SyntaxError를 가지고 있습니다 ... – mgilson

+0

내 잘못 됐어. 인쇄물이있는 한 줄짜리 글꼴은 파이썬에서 훌륭하게 작동하지 않습니다. – Amelia

+1

예이 일은 나를 위해 일했습니다. 고마워요. – Adilicious

1
In [51]: test = ["aaa", "bee", "cee"] 

In [52]: for elem in test: 
    ....:  print "hello,", elem 
    ....: 
hello, aaa 
hello, bee 
hello, cee 
2
In [10]: test = ["aaa", "bee", "cee"] 

In [11]: print "\n".join("hello, "+x for x in test) 
hello, aaa 
hello, bee 
hello, cee 

나 :

In [13]: print "\n".join("hello, {0}".format(x) for x in test) 
hello, aaa 
hello, bee 
hello, cee 
0
 for i in range(len(test)): 
     print "Hello, "+(test[i]); 
+3

'for i in range (len (test))'는'for i in range (test .__ len __()) '보다 확실히 좋습니다. 여기에서는 테스트를 직접 반복하는 것이 훨씬 낫습니다. '테스트에서 elem을 위해 : "Hello"를 출력하고, + elem' – mgilson

+0

'test'는 범위를 지정할 필요없이 직접 반복 할 수 있습니다. ('test .__ iter __()'이 가능합니다) – Amelia