파이썬 format문자 사용시 변수하나로 여러게 사용이 가능하나요?
조회수 1066회
파이썬에서 반복문을 사용할떄 변수하나에 여러 값이 들어가는데 이 변수 하나를 프린트 출력할떄 변수 하나로 지정된 format함수 사용이 가능한지 궁금합니다.
import sqlite3
class test():
def __init__(self):
self.conn = sqlite3.connect('test.db')
self.curosr = self.conn.cursor()
self.conn.execute('create table if not exists test1(id integer primary key autoincrement, name char(5), age char(5))')
self.conn.execute('create table if not exists test2(id integer primary key autoincrement, name char(5), age char(5))')
self.conn.execute('create table if not exists test3(id integer primary key autoincrement, name char(5), age char(5))')
self.conn.execute("insert into test1(id, name, age) values(null, 'andy', '29')")
self.conn.execute("insert into test1(id, name, age) values(null, 'terry', '30')")
self.conn.execute("insert into test2(id, name, age) values(null, 'bab', '11')")
self.conn.execute("insert into test2(id, name, age) values(null, 'john', '24')")
self.conn.execute("insert into test3(id, name, age) values(null, 'intel', '100')")
self.conn.execute("insert into test3(id, name, age) values(null, 'amd', '50')")
for i in range(1, 4):
for j in self.curosr.execute('select *from test{0} order by id desc'.format(i)).fetchmany():
dic={'j{}'.format(i):j}
print("{0},{1},{2} {3},{4},{5} {6},{7},{8}".format(j[0],j[1],j[2],j[0],j[1],j[2],j[0],j[1],j[2]))
test()
-----------print----------
2,terry,30 2,terry,30 2,terry,30
2,john,24 2,john,24 2,john,24
2,amd,50 2,amd,50 2,amd,50
예제 코드에서 print("{0},{1},{2} {3},{4},{5} {6},{7},{8}".format(j[0],j[1],j[2],j[0],j[1],j[2],j[0],j[1],j[2]))
출력시 .format(j[0],j[1],j[2])
이렇게 변경해서 사용하여 출력을2,terry,30 2,john,24 2,amd,50
이렇게 만들고 싶은데 파이썬에서 저런 문법이 가능한지? 궁금합니다.
1 답변
-
가능합니다
print("{0}, {1}, {2} {0}, {1}, {2}, {0}, {1}, {2}" .format(j[0],j[1],j[2]))
이런 식으로 하시면 됩니다
-
(•́ ✖ •̀)
알 수 없는 사용자
-
댓글 입력