Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 在列表中列出元素而不是字符串?_Python - Fatal编程技术网

Python 在列表中列出元素而不是字符串?

Python 在列表中列出元素而不是字符串?,python,Python,我有一个单词标记列表,例如: my_list = [[('abc, 123'), ('def, 456')], [('ghi, 789'), ('abc, 456')]] 我想用正则表达式做一些预处理,比如用一个空格替换,“(例如string=re.sub(r)(,)”,“,”,string)。然后,我想再次将字符串附加到另一个列表中。总之,我想得到一个预处理的列表,如下所示: my_list_preprocessed = [[('abc 123'), ('def 456')], [('g

我有一个单词标记列表,例如:

my_list = [[('abc, 123'), ('def, 456')], [('ghi, 789'), ('abc, 456')]]  
我想用正则表达式做一些预处理,比如用一个空格替换,“(例如string=re.sub(r)(,)”,“,”,string)。然后,我想再次将字符串附加到另一个列表中。总之,我想得到一个预处理的列表,如下所示:

my_list_preprocessed = [[('abc 123'), ('def 456')], [('ghi 789'), ('abc 456')]]
my_list_preprocessed = []
for string in my_list:
  string = re.sub(r"(, )", " ", str(string))
  my_list_preprocessed.append(string)
目前我尝试这样实现它:

my_list_preprocessed = [[('abc 123'), ('def 456')], [('ghi 789'), ('abc 456')]]
my_list_preprocessed = []
for string in my_list:
  string = re.sub(r"(, )", " ", str(string))
  my_list_preprocessed.append(string)
但是,没有得到预期的结果。我收到了一个字符串列表:

my_list = ["['abc 123', 'def 456']", "['ghi 789', 'abc 456']"]

为了得到预期的结果,我可以改进什么?

如果我理解正确,您希望删除分隔字母和数字的逗号。不过,您应该注意的一点是,括号是多余的,因为您拥有的是字符串而不是元组。以下是一些建议的更正,我认为这些更正将有助于您实现您的目标需要

my_list = [[('abc, 123'), ('def, 456')], [('ghi, 789'), ('abc, 456')]]

my_list_preprocessed = []
for item in my_list:
    item_preprocessed = []
    for inner_item in item:
        inner_item = inner_item.replace(",", "")
        item_preprocessed.append(inner_item)
    my_list_preprocessed.append(item_preprocessed)
print(my_list_preprocessed)

您可以使用列表理解来获得您想要的:

my_list = [[('abc, 123'), ('def, 456')], [('ghi, 789'), ('abc, 456')]]

my_list_preprocessed = [[x[0].replace(',', ''), x[1].replace(',','')] for x in my_list]
但是,请注意,此操作的输出为:

[['abc 123', 'def 456'], ['ghi 789', 'abc 456']]
而不是

[[('abc 123'), ('def 456')], [('ghi 789'), ('abc 456')]]
这是因为元组不能由一个项组成。如果确实需要元组,则应使用以下代码:

my_list_preprocessed = [[(x[0].replace(',', ''),), (x[1].replace(',',''),)] for x in my_list]

请注意添加的逗号。它们用于创建仅包含一项的元组。

输入:

my_list = [[('abc, 123'), ('def, 456')], [('ghi, 789'), ('abc, 456')]]  
代码:

import re
my_list_preprocessed = map(lambda x:map(lambda y:re.sub(r',',' ',y),x),my_list)
[['abc  123', 'def  456'], ['ghi  789', 'abc  456']]
输出:

import re
my_list_preprocessed = map(lambda x:map(lambda y:re.sub(r',',' ',y),x),my_list)
[['abc  123', 'def  456'], ['ghi  789', 'abc  456']]

好吧,您对正则表达式的使用过度了。如果您只想用“”替换“,”,那么请执行
mystring=mystring.replace(“,”,”)
如果我理解正确,您的列表是一个字符串项列表。如果要迭代最里面的字符串,需要执行嵌套循环。最外面的循环迭代每个内部列表,最里面的循环迭代该列表中的每个字符串。最里面的
顺便说一句,括号其实是不必要的,除非你想把项目变成一个元组,在这种情况下,你还需要添加一个尾随逗号。你的代码中有太多的打字错误……你确定你是在用
[('abc,123'),('def,456')]
而不是
[('abc','123'),('def','456')]
。您当前显示的
我的列表
是元组中只有一个元素。。我认为您所写的内容是否正确并不重要。@StefanPochmann将所有字符串存储在括号中。…您称之为
('abc,123'))
.string在括号内..我看到它是一个元组..要么是
('abc',123')
要么是
('abc',123')
。但是看到有问题的打字错误,我不确定哪个用户更喜欢…你的代码给出了
['abc 123',def 456',ghi 789',abc 456']
。输出应该是
['abc 123',def 456'],['ghi 789','abc 456'].