【问题标题】:Converting fetch_all array into single array in python [duplicate]在python中将fetch_all数组转换为单个数组[重复]
【发布时间】:2017-07-23 10:04:37
【问题描述】:

在 python 中,我从数据库中获取所有数据。

SENTENCE = "It is wonderful. I'am happy"
sent= (word_tokenize(SENTENCE))

cursor = conn.cursor()
format_strings = ','.join(['%s'] * len(sent))
cursor.execute("SELECT emotion_type FROM emotion WHERE key_word IN (%s)" % format_strings,tuple(sent))
results = cursor.fetchall()
for i in results:
        z= (i)

输出是

('happy',)
('happy',)

但我想得到这个结果

['happy','happy']

如果有任何可能的方式来获得我想要的输出。请帮帮我!

【问题讨论】:

    标签: python arrays


    【解决方案1】:

    如果结果仅来自单个列,那么您总是可以这样做

    results = [res[0] for res in cursor.fetchall()]
    

    【讨论】:

    • 谢谢。这正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-01
    • 1970-01-01
    • 2017-12-19
    • 1970-01-01
    • 1970-01-01
    • 2019-03-20
    相关资源
    最近更新 更多