【问题标题】:Count the number of uppercase and lowercase letters for each item in an array计算数组中每个项目的大写和小写字母的数量
【发布时间】:2020-02-05 06:34:42
【问题描述】:

我正在尝试编写一个函数来计算数组中每个项目的小写和大写字母的数量。然后在我的原始表中创建一个新列,将该实例分类为具有更多大写或小写字母。

这是我的数组:

[array([['MARZIA HAS LIGMA LWIAY #0044'],
   ['This Slinky Montage Is Bizarrely Satisfying to Watch'],
   ['MAKING HER DREAM COME TRUE! (MAKE A WISH)'],
   ...,
   ['THE EVOLUTION OF FORTNITE! 2011 - 2019'],
   ["India's trucks are works of art"],
   ['Several airlines change flight routes after Iranian missile downs American drone']],
  dtype=object)]

我拥有的功能

Upper=0
Lower=0
for c in range(len(dummy_array)):
    for i in c:
        if i.isupper():
            Upper +=1
        elif i.islower():
            Lower +=1
        else:
            pass

        if d[Upper] > d[Lower]:
            train_data["Caps_in_title"] = 1
        else:
            train_data["Caps_in_title"] = 0
print(Upper)
print(Lower)

【问题讨论】:

  • 问题是什么?有什么问题吗?
  • c 是数组索引,而不是数组元素,所以for i in c: 没有意义。您应该使用for c in dummy_array:,而不使用range(len(...))
  • 您有一个嵌套在列表中的多维数组。您需要嵌套循环来获取各个字符。
  • 变量d指的是什么?

标签: python arrays pandas loops count


【解决方案1】:

这应该可行:

import pandas as pd

# setup
df = pd.DataFrame({
    "text" : [['MARZIA HAS LIGMA LWIAY #0044'],
       ['This Slinky Montage Is Bizarrely Satisfying to Watch'],
       ['MAKING HER DREAM COME TRUE! (MAKE A WISH)'],
       ['THE EVOLUTION OF FORTNITE! 2011 - 2019'],
       ["India's trucks are works of art"],
       ['Several airlines change flight routes after Iranian missile downs American drone']]
})

def character_case_ratio(xs):
    """
    function to count number of upper and lowercase letters and compare there ratio
    """
    upper_count = len([x for x in xs[0] if (x.isupper() and x.isalpha())])
    lower_count = len([x for x in xs[0] if (x.islower() and x.isalpha())])

    if upper_count > lower_count:
        return 1
    return 0

# apply the function above over your input text, and create a new column in the DataFrame
df["case_ratio"] = df["text"].apply(character_case_ratio)

输出:

    text                                                case_ratio
0   [MARZIA HAS LIGMA LWIAY #0044]                      1
1   [This Slinky Montage Is Bizarrely Satisfying t...   0
2   [MAKING HER DREAM COME TRUE! (MAKE A WISH)]         1
3   [THE EVOLUTION OF FORTNITE! 2011 - 2019]            1
4   [India's trucks are works of art]                   0
5   [Several airlines change flight routes after I...   0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-11
    • 2014-10-03
    相关资源
    最近更新 更多