简体   繁体   English

在python中对齐列表中的元素

[英]Align elements in list in python

I am trying to get values from database.我正在尝试从数据库中获取值。 When I print;I get below sequence当我打印时;我得到以下序列

- ('Total Row(s):', 4)
- [202]
- [226]
- [561]
- [23]

But I need them to convert in list such as list = [202,226,561,23] but I am unable to get this.但是我需要它们在列表中进行转换,例如list = [202,226,561,23]但我无法得到这个。 All elements are showing on 0th position.所有元素都显示在第 0 个位置。 Can someone please help me?`有人可以帮我吗?`

This is my code and I am connecting with mysql I am connecting with mysql:这是我的代码,我正在与 mysql 连接我正在与 mysql 连接:

import mysql.connector
from mysql.connector import MySQLConnection, Error
conn = mysql.connector.connect(host='examplecom',
                               database='niya')                 
        cursor = conn.cursor()
        old_Value = raw_input("Please enter old Pincode: ")
        print "Pincode is entered by you is : %s" % old_Value
        query_Value = ("select `id`,`type_id`,"
                       "mappings`.`facility_id`,"
                       "mappings`.`service_area_id` from `service_areas`,"
                       "`mappings` where mappings`.`service_area_id`=`id` "
                       "and `type_id`='{0}'".format(old_Value))           
        cursor.execute(query_Value, )
        rows = cursor.fetchall()
        print('Total Row(s):', cursor.rowcount)
        for row in rows:
            list_Id = [];
            facility_Id = int(row[2])
            list_Id.append(facility_Id)
            print list_Id

Change this:改变这个:

for row in rows:
    list_Id = [];
    facility_Id = int(row[2])
    list_Id.append(facility_Id)
    print list_Id

into:进入:

list_Id = []
for row in rows:
    facility_Id = int(row[2])
    list_Id.append(facility_Id)
print list_Id

You made a new list for each row but want one list with all IDs, ie all rows.您为每一行创建了一个新列表,但想要一个包含所有 ID(即所有行)的列表。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM