使用Python分割列表元素

4

在下面的代码中给定列表(listEx),我试图将字符串、整数和浮点数类型分开,并将它们放入各自的列表中。如果我想只从listEx列表中提取字符串,程序应该遍历listEx,并将字符串放入一个名为strList的新列表中,然后将其输出给用户。对于整数和浮点数类型也同样如此。但是,如果我能找到正确的方法来做其中的一个,那么其他的就没问题了。到目前为止还没有成功,已经做了一个小时了。

listEx = [1,2,3,'moeez',2.0,2.345,'string','another string', 55]
strList=['bcggg'] 

for i in listEx:
    if type(listEx) == str:
        strList = listEx[i]
        print strList[i]
    if i not in listEx:
        break
    else:
        print strList

for i in strList:
    if type(strList) == str:
        print "This consists of strings only"
    elif type(strList) != str:
        print "Something went wrong"
    else:
        print "Wow I suck"
5个回答

3
也许可以使用 item.__class__ 来代替 if type(item) == ...,这样可以让 item 告诉你它的类。
import collections
listEx = [1,2,3,'moeez',2.0,2.345,'string','another string', 55]
oftype = collections.defaultdict(list)
for item in listEx:
    oftype[item.__class__].append(item)

for key, items in oftype.items():
    print(key.__name__, items)

产量
int [1, 2, 3, 55]
str ['moeez', 'string', 'another string']
float [2.0, 2.345]

所以,您要查找的三个列表可以分别访问为oftype[int]oftype[float]oftype[str]

2
integers = filter(lambda x: isinstance(x,int), listEx)

strings = filter(lambda x: isinstance(x,str), listEx)

and so on...


2

只需将 type(strList)type(listEx) 更改为 type(i) 即可。您正在遍历列表,但是检查的是 列表 是否为字符串,而不是检查 是否为字符串。


1

Python的for循环迭代实际对象引用。您可能会看到奇怪的行为,部分原因是您正在将对象引用i放在应该是数字列表索引的位置(语句listEx[i]没有意义。数组索引可以是i = 0…length_of_list的值,但有一点i="moeez")

您还每次找到一个项目时都要替换整个列表(strList = listEx[i])。您可以使用strList.append(i)将新元素添加到列表末尾,但这里有一个更简洁,稍微更pythonic的选择,它使用一个非常有用的python构造列表推导式一行创建整个列表。

listEx = [1,2,3,'moeez',2.0,2.345,'string','another string', 55]
strList = [ i for i in listEx if type(i) == str ] 

提供:

print strList
>>> print strList
['moeez', 'string', 'another string']

其余的内容,请参考。

>>> floatList = [ i for i in listEx if type(i) == float ] 
>>> print floatList
[2.0, 2.345]

>>> intList = [ i for i in listEx if type(i) == int ] 
>>> intList
[1, 2, 3, 55]

>>> remainders = [ i for i in listEx 
    if ( ( i not in  strList ) 
          and (i not in  floatList ) 
          and ( i not in intList) )  ]
>>> remainders
[]

这个可行!非常感谢。现在只有最后一件事。你能否带我逐步了解当解释器执行“i for i in listEx if type(i) == str”时会发生什么?我有点明白,但不是完全理解。 - user1819786
我添加了一个链接到一个简单的教程页面,描述了这个过程-这有帮助吗?本质上,这是一个“for each”循环,在其中您可以直接迭代列表中的对象(而不是使用数组索引作为循环变量)。官方的Python文档实际上非常好:http://docs.python.org/2/tutorial/controlflow.html#for-statements - abought

-1
     python 3.2
     listEx = [1,2,3,'moeez',2.0,2.345,'string','another string', 55]

     strList = [ i for i in listEx if type(i) == str ] 
     ## this is list comprehension ##

     ###  but you can use conventional ways.

     strlist=[]                   ## create an empty list.          
     for i in listex:             ## to loop through the listex.
             if type(i)==str:     ## to know what type it is
                    strlist.append(i)        ## to add string element
     print(strlist)    



     or:
     strlist=[]
     for i in listex:
            if type(i)==str:
                strlist=strlist+[i]

     print(strlist)  

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接