【刷题】Leetcode1683. Invalid Tweets

Question

Table: Tweets

+----------------+---------+
| Column Name    | Type    |
+----------------+---------+
| tweet_id       | int     |
| content        | varchar |
+----------------+---------+
tweet_id is the primary key (column with unique values) for this table.
This table contains all the tweets in a social media app.

Write a solution to find the IDs of the invalid tweets. The tweet is invalid if the number of characters used in the content of the tweet is strictly greater than 15.

Return the result table in any order.

The result format is in the following example.

MySQL Solutions

SELECT
    tweet_id
FROM
    Tweets
WHERE
    LENGTH(content) > 15;
SELECT
    tweet_id
FROM
    Tweets
WHERE
    CHAR_LENGTH(content) > 15;

Pandas Solution

  • apply(lambda x: foo(x) ? condition)
def invalid_tweets(tweets: pd.DataFrame) -> pd.DataFrame:
	temp = tweets[tweets['content'].apply(lambda x: len(x)>15)]
	return temp[['tweet_id']]
  • Series.str.len()
    • compute the length of each element in the Series/Index
    • the element can be a sequence (e.g., string, tuple, or list) or a collection (e.g., dictionary)
def invalid_tweets(tweets: pd.DataFrame) -> pd.DataFrame:
    temp = tweets[tweets['content'].str.len()>15]
    return temp[['tweet_id']]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值