4

次のコードのリスト (listEx) を使用して、string 型と integer 型と float 型を分離し、それらすべてをそれぞれのリストに入れようとしています。listEx リストからのみ文字列を抽出したい場合、プログラムは listEx を通過し、文字列を strList という新しいリストに入れ、それをユーザーに出力する必要があります。整数型と浮動小数点型についても同様です。しかし、1つだけを行う正しい方法を理解できれば、他の人には問題ありません. これまでのところ運が悪く、これで 1 時間経過しました。

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"
4

5 に答える 5

3

おそらく、 の代わりにif type(item) == ...を使用item.__class__して、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]

したがって、探している 3 つのリストにはoftype[int]oftype[float]およびとしてアクセスできますoftype[str]

于 2012-11-13T03:02:26.227 に答える
2
integers = filter(lambda x: isinstance(x,int), listEx)

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

等々...

于 2012-11-13T03:24:48.233 に答える
2

type(strList)type(listEx)を に変更するだけtype(i)です。リストを繰り返し処理していますが、アイテムが文字列であるかどうかではなく、リストが文字列であるかどうかを確認しています。

于 2012-11-13T02:59:18.210 に答える
1

Pythonforループは、実際のオブジェクト参照を反復処理します。オブジェクト参照 i に、数値リスト インデックスを配置する必要があるため、奇妙な動作が見られる場合があります (このステートメントlistEx[i]は意味がありません。配列インデックスは i = 0...length_of_list の値にすることができますが、ある時点で i= 「もえ」)

また、項目を見つけるたびにリスト全体を置き換えています ( strList = listEx[i])。代わりに を使用してリストの末尾に新しい要素を追加することもできますが、リスト内包strList.append(i)表記と呼ばれる非常に便利な python 構造を使用して 1 行でリスト全体を作成する、より簡潔で少し 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
[]
于 2012-11-13T03:07:11.947 に答える
-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)  
于 2012-11-13T15:39:37.773 に答える