LoginSignup
4

More than 5 years have passed since last update.

リスト内のタプルを消去する方法 (Python)

Last updated at Posted at 2017-05-06

リスト内のタプルを消去する方法です。

早速コード

models というリストの中から
('Perceptron', 0.52953807740324599) というタプルを消去したい。


models = [(name, score) for name, score in models if name != 'Perceptron']

# models という中の、(name, score)の部分の、name が
#'Perceptron'じゃないやつだけ残しますよってことです。

ちなみに、models の中身はこんな感じ、

[('KNeighbors',
  KNeighborsClassifier(algorithm='auto', leaf_size=30, metric='minkowski',
             metric_params=None, n_jobs=1, n_neighbors=5, p=2,
             weights='uniform')),
 ('DTC',
  DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
              max_features=None, max_leaf_nodes=None,
              min_impurity_split=1e-07, min_samples_leaf=1,
              min_samples_split=2, min_weight_fraction_leaf=0.0,
              presort=False, random_state=None, splitter='best')),
 ('SVM', SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
    decision_function_shape=None, degree=3, gamma='auto', kernel='rbf',
    max_iter=-1, probability=False, random_state=None, shrinking=True,
    tol=0.001, verbose=False)),
 ('LinearSVC',
  LinearSVC(C=1.0, class_weight=None, dual=True, fit_intercept=True,
       intercept_scaling=1, loss='squared_hinge', max_iter=1000,
       multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
       verbose=0))]

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4