python: 在对象列表中搜索某个属性值
python: searching list of objects for certain attribute value
我有一个 class 看起来像这样的 Twitter 推文,我认为可能有一个很好的解决方法
class tweet:
def __init__(self, userName, likes, replys, retweets, message, ID):
self.userName = userName
self.likes = likes
self.replys = replys
self.retweets = retweets
self.message = message
self.lenMessage = len(self.message)
self.mentions = self.message.count('@')
self.hastags = self.message.count('#')
self.ID = ID
在我的 main class
中有一个此类对象的列表。我希望能够在此列表中查看列表中的推文中是否有任何具有特定 ID 的推文,但我只是想不出具体如何表述:
from Students import tweet
listoftweets = []
t1 = tweet("ding", 100, 5, 1,"a message", 1906961683)
t2 = tweet("dong" ,5, 5, 5, "another message", 190696969)
t3 = tweet("bang", 1, 1, 1, "anew message",2003)
t4 = tweet("fest" ,3, 3, 3,"a message", 1930)
listoftweets.append(t1)
listoftweets.append(t2)
listoftweets.append(t3)
listoftweets.append(t4)
# now if i wanted to check if there is a object with the attribute ID = 2003
#how would i do this?
if listoftweets.__contains__():
print("the list does contain specified ID")
非常感谢大家
这是您要找的吗?
for l in listoftweets:
if l.ID == "###": # ID to be matched
print("List does contain specified ID")
break
这样的方法可以帮助:
def get_tweet(id):
for tweet in listoftweets:
if tweet.ID == id:
return tweet
建议:改用namedtuples
。以下作品:
from collections import namedtuple
class Base:
@property
def lenMessage(self):
return len(self.message)
@property
def mentions(self):
return self.message.count('@')
@property
def hashtags(self):
return self.message.count('#')
class Tweet(Base, namedtuple('BaseTweet', 'userName likes replies retweets message ID')):
pass
listoftweets = [
Tweet("ding", 100, 5, 1,"a message", 1906961683),
Tweet("dong" ,5, 5, 5, "another message", 190696969),
Tweet("bang", 1, 1, 1, "anew message",2003),
Tweet("fest" ,3, 3, 3,"a message", 1930)
]
found = [tweet for tweet in listoftweets if tweet.ID == 2003]
print(found)
这会产生
[Tweet(userName='bang', likes=1, replies=1, retweets=1, message='anew message', ID=2003)]
我有一个 class 看起来像这样的 Twitter 推文,我认为可能有一个很好的解决方法
class tweet:
def __init__(self, userName, likes, replys, retweets, message, ID):
self.userName = userName
self.likes = likes
self.replys = replys
self.retweets = retweets
self.message = message
self.lenMessage = len(self.message)
self.mentions = self.message.count('@')
self.hastags = self.message.count('#')
self.ID = ID
在我的 main class
中有一个此类对象的列表。我希望能够在此列表中查看列表中的推文中是否有任何具有特定 ID 的推文,但我只是想不出具体如何表述:
from Students import tweet
listoftweets = []
t1 = tweet("ding", 100, 5, 1,"a message", 1906961683)
t2 = tweet("dong" ,5, 5, 5, "another message", 190696969)
t3 = tweet("bang", 1, 1, 1, "anew message",2003)
t4 = tweet("fest" ,3, 3, 3,"a message", 1930)
listoftweets.append(t1)
listoftweets.append(t2)
listoftweets.append(t3)
listoftweets.append(t4)
# now if i wanted to check if there is a object with the attribute ID = 2003
#how would i do this?
if listoftweets.__contains__():
print("the list does contain specified ID")
非常感谢大家
这是您要找的吗?
for l in listoftweets:
if l.ID == "###": # ID to be matched
print("List does contain specified ID")
break
这样的方法可以帮助:
def get_tweet(id):
for tweet in listoftweets:
if tweet.ID == id:
return tweet
建议:改用namedtuples
。以下作品:
from collections import namedtuple
class Base:
@property
def lenMessage(self):
return len(self.message)
@property
def mentions(self):
return self.message.count('@')
@property
def hashtags(self):
return self.message.count('#')
class Tweet(Base, namedtuple('BaseTweet', 'userName likes replies retweets message ID')):
pass
listoftweets = [
Tweet("ding", 100, 5, 1,"a message", 1906961683),
Tweet("dong" ,5, 5, 5, "another message", 190696969),
Tweet("bang", 1, 1, 1, "anew message",2003),
Tweet("fest" ,3, 3, 3,"a message", 1930)
]
found = [tweet for tweet in listoftweets if tweet.ID == 2003]
print(found)
这会产生
[Tweet(userName='bang', likes=1, replies=1, retweets=1, message='anew message', ID=2003)]