如何忽略对python词典中的某个单词进行计数

时间:2019-03-16 19:31:54

标签: python

您好,我想知道如何制作一个不计单词'the'的字典,或者绝对不将其从字典中删除,所以我想出了这段代码:

counts = dict()
print('Enter a line of text:')
line = input('')
words = line.split()
print('Words:', words)
print('counting...')
for word in words :
    if words != 'the':
        counts[word]= counts.get(word,0)+1
    else:
        counts[word] = counts.get(word,0)
print('Counts', counts)

您能帮我做对吗?

2 个答案:

答案 0 :(得分:1)

@Steve的答案是正确的,但是代码可以美化和简化:

from collections import Counter

line = input('Enter a line of text:')
words = line.split()

print('Words:', words)
print('counting...')

c = Counter(words)
del c['the'] # remove 'the' key from counter

print('Counts', dict(c))

答案 1 :(得分:0)

为什么不仅如此? :

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".ui.events.ScrollingActivity">

<com.google.android.material.appbar.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true">

    <net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:maxLines="3"
        app:titleEnabled="true">

        <ImageView
            android:id="@+id/iv_banner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:adjustViewBounds="true"
            android:src="@drawable/add_event_bg_gradient"
            app:contentScrim="@color/colorPrimary"
            app:layout_collapseMode="parallax"
            tools:ignore="contentDescription" />

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android:adjustViewBounds="true"
            android:src="@drawable/grey_bg_gradient"
            app:contentScrim="@color/colorPrimary"
            app:layout_collapseMode="parallax"
            tools:ignore="contentDescription" />

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </net.opacapp.multilinecollapsingtoolbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>

<com.google.android.material.floatingactionbutton.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    app:layout_anchor="@id/app_bar"
    app:layout_anchorGravity="bottom|end"
    app:srcCompat="@drawable/ic_edit_vector"
    tools:ignore="VectorDrawableCompat" />

<include layout="@layout/Nested scrolling layout" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>