2016-11-17 7 views
0

文字列内の文字を繰り返し処理していますが、各文字の種類を判別したいと思います。コードはこのようなものだった場合True/Falseステートメントを使用してif/elseブロックを辞書に変換する

if char.isdigit(): 
     char_type = 'number' 
    elif char.isalpha(): 
     char_type = 'letter' 
    elif char in {'*', '/', '+', '-', '='}: 
     char_type = 'operator' 
    else: 
     char_type = 'other' 

これがあれば/ elseブロックで行うことができます

if val == 1: 
     char_type = 'number' 
    elif val == 2: 
     char_type = 'letter' 
    elif val == 3: 
     char_type = 'operator' 

辞書は次のようになります。

d = {1: 'number', 2: 'letter', 3: 'operator'} 

しかし、この場合、それぞれの文はTrueまたはFalseです。これを辞書に変換できますか?

+0

私はあなたが鍵を欲しいとは思っていません。これで何をしようとしていますか? – JETM

+0

@ JETMおそらく、 'if'ステートメントの代わりに' char_type = d [char] 'のようなものでしょう。 –

+2

正直、ちょうど関数でラップし、 'char_type(char)'を呼び出してください。 'char_type [char]'と比較して読みやすさの節約は全くありません。コードを読んだ人を混乱させるだけです。 –

答えて

1

はい、d[char]の構文を使用できますが、複雑さと読みやすさの面でコストがかかりません。

あなたはクラスの__getitem__方法であなたのif/elseをカプセル化して、文字の種類を取得するために[]構文を使用し、そのような可能性:

class CharType: 
    def __getitem__(self, char): 
     if char.isdigit(): 
      char_type = 'number' 
     elif char.isalpha(): 
      char_type = 'letter' 
     elif char in {'*', '/', '+', '-', '='}: 
      char_type = 'operator' 
     else: 
      char_type = 'other' 
     return char_type 

d = CharType() 

print(d["a"], d["+"]) 
-1

私はあなたの質問が理解するものによります。例列出力用

s="abcd12e+" 
dic={} 
for i in s: 
    if i.isdigit(): 
     dic[i]="digit" 
    elif i.isalpha(): 
     dic[i]="alpha" 
    elif i in ['*', '/', '+', '-', '=']: 
     dic[i]="operator" 
    else: 
     dic[i]="other" 
print dic 

こと: { 'A' 'α'、 'C​​': 'α'、 'B': 'α'、 'E': 'α'、D」 ':' alpha '、' + ':' operator '、' 1 ':' digit '、' 2 ':' digit '}

0

はい、すべての作業を行うように辞書を設定できます。セットアップしたら、あなたは非常に迅速にそれを介してテキストの多くを実行できます。

import string 

type_dictionary = dict() 
type_dictionary.update({c: 'letter' for c in string.ascii_letters}) 
type_dictionary.update({c: 'number' for c in string.digits}) 
type_dictionary.update({c: 'operator' for c in {'*', '/', '+', '-', '='}}) 

string = "elif val == 3:" 

for character in string: 
    print(character, "->", type_dictionary.get(character, 'other')) 

OUTPUT

e -> letter 
l -> letter 
i -> letter 
f -> letter 
    -> other 
v -> letter 
a -> letter 
l -> letter 
    -> other 
= -> operator 
= -> operator 
    -> other 
3 -> number 
: -> other 

は別のアプローチは、文字変換テーブルを使用することです:

import string 
import itertools 

type_dictionary = {'1': 'number', '2': 'letter', '3': 'operator', '4': 'other'} 

maketrans_dictionary = dict({c: '4' for c in string.printable}) 
maketrans_dictionary.update({c: '2' for c in string.ascii_letters}) 
maketrans_dictionary.update({c: '1' for c in string.digits}) 
maketrans_dictionary.update({c: '3' for c in {'*', '/', '+', '-', '='}}) 

table = str.maketrans(maketrans_dictionary) 

string = "elif val == 3:" 

for character, character_type in zip(string, string.translate(table)): 
    print(character, "->", type_dictionary[character_type]) 

のどちら上記のコードはUnicodeに対応しており、追加作業は必要ありませんが、どちらも問題ありません。char in {'*', '/', '+', '-', '='}です。

+0

これは "ñ"で失敗しませんか? –

+0

いいえ、@Robᵩ、 "ñ - > other"を表示します。 – cdlane

+0

しかし、OPの元のコードの意図と一致するように、 "* - 文字"を出力するべきです*。 –

関連する問題