试图在字符串

时间:2017-06-06 23:48:08

标签: python-3.x

我有以下功能,我正在尝试制作一个包含'推文号码的列表。由DanClark完成的推文。我正在拆分字符串并尝试使用过滤器函数来生成它。我试图避免显式递归。我一直收到以下错误" ' STR'对象不可调用"当我尝试运行" search_tweets(推文," DanClark")"

tweet1 = "#1::@DanClark::The party was amazing" 
tweet19 = "#19::@NatalyS::Avoid 401 Toronto area at this time" 
tweet50 = "#50::@CBCNews::How Canadian captain gave her team a speech" 
tweet14 = "#14::@DanClark::The food was good"
tweet15 = "#15::@DaveLin::Lucky you DanClark"
tweets =   [tweet1, tweet19, tweet50, tweet14, tweet15]

def separator(tweets, item1, item2, tweeter):

part1 = (tweets[item1].split('::'))
part2 = part1[item2]
part3 = part2[1:]

  if part3 == tweeter:
      return (part1[0])[1:]
  else:
      return []

def search_tweets(tweets, tweeter):
  return list(filter((separator(tweets, 0, 1, tweeter)), tweets))

4 个答案:

答案 0 :(得分:0)

我相信filter希望第一个参数是一个函数,而你给它调用分隔符的返回值。

来自python docs:

请注意,filter(function,iterable)等效于生成器表达式(如果function(item),则为iterable中的item项)

答案 1 :(得分:0)

tweet1 = "#1::@DanClark::The party was amazing" 
tweet19 = "#19::@NatalyS::Avoid 401 Toronto area at this time" 
tweet50 = "#50::@CBCNews::How Canadian captain gave her team a speech" 
tweet14 = "#14::@DanClark::The food was good"
tweet15 = "#15::@DaveLin::Lucky you DanClark"
tweets = [tweet1, tweet19, tweet50, tweet14, tweet15]

def numbers_of_tweets_done_by(who):
    r_list = []
    for tweet in tweets:
        t_info = tweet.split('::')
        if t_info[1] == who:
           r_list.append(int(t_info[0][1:]))
    return r_list

if __name__ == '__main__':
    print(numbers_of_tweets_done_by('@DanClark'))

#Prints [1,14]

答案 2 :(得分:0)

假设:

>>> tweet1 = "#1::@DanClark::The party was amazing" 
>>> tweet19 = "#19::@NatalyS::Avoid 401 Toronto area at this time" 
>>> tweet50 = "#50::@CBCNews::How Canadian captain gave her team a speech" 
>>> tweet14 = "#14::@DanClark::The food was good"
>>> tweet15 = "#15::@DaveLin::Lucky you DanClark"
>>> tweets =   [tweet1, tweet19, tweet50, tweet14, tweet15]

您可以使用filter按照第一个元素搜索列表tweets以获取推文:

>>> tweeter = '#50'
>>> list(filter(lambda s: s.partition('::')[0]==tweeter, tweets))
['#50::@CBCNews::How Canadian captain gave her team a speech']

或第二个元素:

>>> tweeter = '@DanClark'
>>> list(filter(lambda s: s.split('::')[1]==tweeter, tweets))
['#1::@DanClark::The party was amazing', '#14::@DanClark::The food was good']

然后,如果您只想要与Dan的推文相对应的数字:

>>> [e.partition('::')[0] for e in filter(lambda s: s.split('::')[1]==tweeter, tweets)]
['#1', '#14']

答案 3 :(得分:0)

如果你真的坚持不使用显式循环,列表推导或任何类似的东西,你可以将列表的每个元素映射到搜索功能,然后过滤掉空结果。类似的东西:

def search_tweets(tweets, author):

    def search_tweet(tweet):
        tweet_fields = tweet.split('::', 2)
        return tweet_fields[0][1:] if tweet_fields[1][1:] == author else None

    return list(filter(None, map(search_tweet, tweets)))

然后,如果您提供数据并与目标作者联系,您将获得预期结果:

tweet1 = "#1::@DanClark::The party was amazing"
tweet19 = "#19::@NatalyS::Avoid 401 Toronto area at this time"
tweet50 = "#50::@CBCNews::How Canadian captain gave her team a speech"
tweet14 = "#14::@DanClark::The food was good"
tweet15 = "#15::@DaveLin::Lucky you DanClark"
tweets = [tweet1, tweet19, tweet50, tweet14, tweet15]

print(search_tweets(tweets, "DanClark"))
# prints: ['1', '14']

顺便说一下。 (意见时间)这是一种愚蠢的做法,但无论如何......我听说白痴教师和采访者要求更多荒谬的东西。