如何替换/卸下方括号& python列表中的bouble引号

发布于 2025-01-27 13:54:34 字数 357 浏览 2 评论 0 原文

我需要从列表列表中删除方括号(列表包含子列表)

list = [["'deliver","'ordered","'delivery","'delivering","'brought"],["'deliver","'offering","'providing","'delivers","'orders"]]

示例输出

list = 'deliver','ordered','delivery','delivering','brought'

I need to remove Square Brackets from list of list (list contains sub list's)

list = [["'deliver","'ordered","'delivery","'delivering","'brought"],["'deliver","'offering","'providing","'delivers","'orders"]]

Sample output

list = 'deliver','ordered','delivery','delivering','brought'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ゞ花落谁相伴 2025-02-03 13:54:34

播放列表理解,这就是它的

[print(i) for i in [','.join(x).replace("'",'').split(',') for  x in lst]]

['deliver', 'ordered', 'delivery', 'delivering', 'brought']
['deliver', 'offering', 'providing', 'delivers', 'orders']

目的

[','.join(x).replace("'",'').split(',') for  x in lst][0]

Play with list comprehension, thats what its there for

[print(i) for i in [','.join(x).replace("'",'').split(',') for  x in lst]]

['deliver', 'ordered', 'delivery', 'delivering', 'brought']
['deliver', 'offering', 'providing', 'delivers', 'orders']

Or you could just list slice after you edit using list comprehension

[','.join(x).replace("'",'').split(',') for  x in lst][0]
寄居人 2025-02-03 13:54:34

存在嵌套列表中,则将删除双打

 _list = [["'deliver","'ordered","'delivery","'delivering","'brought"],["'deliver","'offering","'providing","'delivers","'orders"]]

print(', '.join([*set([item for sublist in _list for item in sublist])]))

如果按照正常方式

print(', '.join([item for sublist in _list for item in sublist]))

set will remove doublicates if exist in your nested list

 _list = [["'deliver","'ordered","'delivery","'delivering","'brought"],["'deliver","'offering","'providing","'delivers","'orders"]]

print(', '.join([*set([item for sublist in _list for item in sublist])]))

as normal way

print(', '.join([item for sublist in _list for item in sublist]))
挖鼻大婶 2025-02-03 13:54:34

您是否想要重复的?从这个问题中不清楚。

如果您不在乎具有重复项,请使用列表理解:

print([string.strip("\'") for sublist in lst for string in sublist])

如果您不想要重复项,请使用集合理解并将其转换回列表:

print(list({string.strip("\'") for sublist in lst for string in sublist}))

在这两个示例中,我们使用使用每个字符串的内部单引号剥离方法,一旦将其删除,Python会自动将双引号转换为单引号。但是,有双引号或单引号无关紧要,结果列表中的项目仍然具有字符串。在此处提到的情况下,什么样的引号很重要:

https://www.geeksforgeeks.orgs.org/single-and-and-double-quotes-python/#:~:~: 2c%20 double%20 Quotes%20ARE%20使用,1%20 type%20over%20 the%20other

Do you want duplicates or not? It's unclear from the question.

If you don't care about having duplicates, use a list comprehension:

print([string.strip("\'") for sublist in lst for string in sublist])

If you don't want duplicates, then use a set comprehension and convert it back to a list:

print(list({string.strip("\'") for sublist in lst for string in sublist}))

In both examples, we remove the inner single quotation from each string using the strip method, and once it is removed, python automatically converts the double quotes to single quotes. However, the fact that there are double quotes or single quotes doesn't matter, the items in the resulting lists still have type string. What kind of quotations only matters in cases mentioned here:

https://www.geeksforgeeks.org/single-and-double-quotes-python/#:~:text=Generally%2C%20double%20quotes%20are%20used,one%20type%20over%20the%20other.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文