如何将单元素元组转换为字符串? [英] How to convert single element tuple into string?

查看:25
本文介绍了如何将单元素元组转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码:

 导入 nltk导入pypyodbc文本 = raw_input()token = nltk.word_tokenize(text)//返回一个列表值定义搜索(自我,列表):如果不是 self.connected:self.connect()对于列表中的单词:self.cur.execute('SELECT Ybanag FROM Words WHERE English IN (%s)' % (','.join('?'*len(lists))), 列表)结果 = self.cur.fetchall()返回结果

其中输出是单元素元组的列表(例如,我输入 we all there):[('tore',), ('ngaming',), ('sittam',)](将输入翻译成母语).我希望将输出转换为字符串以消除 [],(),'',' 符号.如何将其转换为字符串?

解决方案

你必须使用 str.join 方法.

<预><代码>>>>a = [('tore',), ('ngaming',), ('sittam',)]>>>" ".join([x[0] for x in a])'撕裂 ngaming 西塔姆'

I have this code:

 import nltk
 import pypyodbc

 text = raw_input()
 token = nltk.word_tokenize(text) //return a list value


def search(self, lists):
    if not self.connected:
        self.connect()
    for word in lists:
        self.cur.execute('SELECT Ybanag FROM Words WHERE English IN (%s)' % (','.join('?'*len(lists))), lists)
        result = self.cur.fetchall()
        return result

wherein the output is a list of single element tuple (ex. I enter we all there): [('tore',), ('ngaming',), ('sittam',)] (translate the input into mother tongue language). I want that the output will be converted into string to eliminate the [],(),'',' symbols. How to convert it into string?

解决方案

You have to use str.join method.

>>> a = [('tore',), ('ngaming',), ('sittam',)]
>>> " ".join([x[0] for x in a])
'tore ngaming sittam'

这篇关于如何将单元素元组转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆