简体   繁体   English

使用 python 获取正确列表项/值的问题

[英]Issue in getting the correct list items/values using python

I have a list in python like this:我在 python 中有一个列表,如下所示:

list1 = ['Security Name % to Net Assets* DEBENtURES 0.04, Britannia Industries Ltd. EQUity & RELAtED 96.83, HDFC Bank 6.98, ICICI 4.82']

if I get the length of this list using len(list1) then it gives as 1 simply because it considers all as 1.如果我使用len(list1)得到这个列表的长度,那么它给出 1 仅仅是因为它认为所有都为 1。

How can I transform my list1 such that it would look like this:如何转换我的 list1 使其看起来像这样:

list1_altered = ['Security Name % to Net Assets* DEBENtURES 0.04', 'Britannia Industries Ltd. EQUity & RELAtED 96.83', 'HDFC Bank 6.98', 'ICICI 4.82']

after which upon using len(list1_altered) I should be able to get value as 4之后使用len(list1_altered)我应该能够将值设为 4

I have tried using replace(",", "\',\'") however it doesn't give the desired result.我曾尝试使用replace(",", "\',\'")但它没有给出预期的结果。

Please help me do this.请帮我做这件事。

Replacing the commas by quotes is not going to change the fact that you'll still have a single string.用引号替换逗号不会改变您仍然只有一个字符串的事实。 You won't transform an object (string) into another (list of strings) this way.您不会以这种方式将 object(字符串)转换为另一个(字符串列表)。

Use str.split to generate a list of multiple strings split on a separator:使用str.split生成在分隔符上拆分的多个字符串的列表:

list1_altered = list1[0].split(',')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM