如何将字符串和列表的unicode组合到python列表中
我有这些独角兽:如何将字符串和列表的unicode组合到python列表中,python,list,unicode,Python,List,Unicode,我有这些独角兽: uni_list = u'["","aa","bb","cc"] uni_str = u'dd' 我还有以下Unicode: uni_list = u'["","aa","bb","cc"] uni_str = u'dd' 我需要将它们组合到一个列表中,去掉空的,期望的结果如下: ["aa","bb","cc","dd"] 但我不知道什么时候它会是一个uni_列表,或者uni_str,因为我正在阅读一个json文件来分割这些结果,是否有一个统一的解决方案来转换它们并将
uni_list = u'["","aa","bb","cc"]
uni_str = u'dd'
我还有以下Unicode:
uni_list = u'["","aa","bb","cc"]
uni_str = u'dd'
我需要将它们组合到一个列表中,去掉空的,期望的结果如下:
["aa","bb","cc","dd"]
但我不知道什么时候它会是一个uni_列表,或者uni_str,因为我正在阅读一个json文件来分割这些结果,是否有一个统一的解决方案来转换它们并将它们合并到python列表或集合中
我试着使用ast.literal_eval,它似乎只处理uni_列表,但当它是uni_str时,给了我“格式错误的字符串”的错误
非常感谢 您可以使用将字符串转换为列表:
>>> import ast
>>> my_unicode = u'["","aa","bb","cc"]'
# convert string to list
>>> my_list = ast.literal_eval(my_unicode)
>>> my_list
['', 'aa', 'bb', 'cc']
# Filter empty string from list
>>> new_list = [i for i in my_list if i]
>>> new_list
['aa', 'bb', 'cc']
# append `"dd"` string to the list
>>> new_list.append("dd") # OR, `str(u"dd")` if `"dd"` is unicode string
>>> new_list
['aa', 'bb', 'cc', 'dd']
您可以使用将字符串转换为列表:
>>> import ast
>>> my_unicode = u'["","aa","bb","cc"]'
# convert string to list
>>> my_list = ast.literal_eval(my_unicode)
>>> my_list
['', 'aa', 'bb', 'cc']
# Filter empty string from list
>>> new_list = [i for i in my_list if i]
>>> new_list
['aa', 'bb', 'cc']
# append `"dd"` string to the list
>>> new_list.append("dd") # OR, `str(u"dd")` if `"dd"` is unicode string
>>> new_list
['aa', 'bb', 'cc', 'dd']
使用和功能的替代解决方案:
result = []
def getValues(s):
global result
// check if input string contains list representation
is_list = re.match(r'^\[[^][]+\]$', s, re.UNICODE)
if is_list:
result = result + re.findall(r'\"([^",]+)\"', s, re.UNICODE)
else:
result.append(s)
getValues(u'["","aa","bb","cc"]')
getValues(u'dd')
print(result)
输出:
['aa', 'bb', 'cc', 'dd']
使用和功能的替代解决方案:
result = []
def getValues(s):
global result
// check if input string contains list representation
is_list = re.match(r'^\[[^][]+\]$', s, re.UNICODE)
if is_list:
result = result + re.findall(r'\"([^",]+)\"', s, re.UNICODE)
else:
result.append(s)
getValues(u'["","aa","bb","cc"]')
getValues(u'dd')
print(result)
输出:
['aa', 'bb', 'cc', 'dd']
... 没有“列表的unicode”这样的东西。可以使用表示列表的unicode字符串。您可以有一个unicode字符串列表。你现在的处境真的不清楚。你能发表一些说明你的问题的文章吗?请展示你迄今为止所做的尝试以及你正在使用的输入
u'[“”,“aa”,“bb”,“cc”]
既不是有效的unicode字符串,也不是有效的列表。如果您从json获取该字符串,则会遇到其他问题,例如谁首先将该字符串写入json。。。。没有“列表的unicode”这样的东西。可以使用表示列表的unicode字符串。您可以有一个unicode字符串列表。你现在的处境真的不清楚。你能发表一些说明你的问题的文章吗?请展示你迄今为止所做的尝试以及你正在使用的输入u'[“”,“aa”,“bb”,“cc”]
既不是有效的unicode字符串,也不是有效的列表。如果您是从json获取该字符串,则会遇到其他问题,例如是谁首先将该字符串写入json的。我试过ast.literal\u eval,问题是我不知道何时会有u“dd”或u'[“”,“aa”,“bb”,“cc”],所以,当我对所有的字符串执行ast.literal\u eval时,当它是一个u“dd”时,它会给我一个错误“格式错误的字符串”,你知道如何解决这个问题吗?谢谢但是您确定字符串将只属于这两种类型吗?i、 e.u“dd”或u'[“”、“aa”、“bb”、“cc”]?使用过滤器删除列表中的空元素
比列表理解更快new_list=filter(None,my_list)
对于我的数据,是的,在json.loads()之后,我有u“aa”或u'[]',或u'[“aa”,“bb”]',但我不知道它是其中之一3@MaxChr埃蒂安,事实并非如此。至少在Python2.7中没有。在Python3.x中,它会更快,因为它返回迭代器对象。如果您键入cast it to list,它将再次变慢。我尝试了ast.literal\u eval,问题是我不知道什么时候我有一个u“dd”或u“[”,“aa”,“bb”,“cc”]”,所以当我对所有这些进行ast.literal\u eval时,当它是u“dd”时,它会给我一个错误“格式错误的字符串”,您知道如何解决这个问题吗?谢谢但是您确定字符串将只属于这两种类型吗?i、 e.u“dd”或u'[“”、“aa”、“bb”、“cc”]?使用过滤器删除列表中的空元素
比列表理解更快new_list=filter(None,my_list)
对于我的数据,是的,在json.loads()之后,我有u“aa”或u'[]',或u'[“aa”,“bb”]',但我不知道它是其中之一3@MaxChr埃蒂安,事实并非如此。至少在Python2.7中没有。在Python3.x中,它会更快,因为它返回迭代器对象。如果您键入cast to list,它将再次变慢