浏览词典列表并仅选择存在键的行

时间:2015-05-04 21:14:31

标签: python dictionary

如何搜索词典列表,只选择存在某个键的行(无论值是多少)?例如,在这三行中,我只想保留那些存在"userid"的行(最后两行)。

[{'long': None, 'tweetid': None, 'timestamp': None, 'lat': None},
 {'userid': '113503286', 'long': '-87.624387', 'tweetid': '595084326489956352', 'timestamp': '1430714134224', 'lat': '41.852653'}, 
 {'userid': '2421024865', 'long': '-87.629798', 'tweetid': '595084376632729600', 'timestamp': '1430714146179', 'lat': '41.878114'}]

我尝试了以下代码,但收到错误:

datum = [ row for row in data if row['tweet_time'] in row]
Traceback (most recent call last):
File "<pyshell#102>", line 1, in <module>
datum = [ row for row in data if ['tweet_time'] in row]

4 个答案:

答案 0 :(得分:5)

[row for row in data if row['tweet_time'] in row]

不。但是几乎。对dict的遏制检查检查密钥是否存在。

[row for row in data if 'userid' in row]

答案 1 :(得分:3)

lambda函数也有效......

SELECT COUNT(*)
FROM [githubarchive:github.timeline]
WHERE type = 'PushEvent' 
    AND repository_name = "account/repo"
GROUP BY pushed_at
ORDER BY pushed_at DESC

答案 2 :(得分:1)

并且在“过度设计的解决方案”类别中争夺第一名:亚当·史密斯提交了名为嘿的这篇文章,让我们使用数据库!

# /models.py

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, Numeric

Base = declarative_base()

class Tweet(Base):
    __tablename__ = "tweets"
    id = Column(Integer, primary_key=True)
    userid = Column(Integer, nullable=False)
    tweetid = Column(Integer, nullable=False)
    timestamp = Column(Integer, nullable=False)
    long = Column(Numeric, nullable=False)
    lat = Column(Numeric, nullable=False)

# /main.py

from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy.ext import IntegrityError
from models import Tweet, Base

PATH_TO_DB = "tweet_info.db"

engine = create_engine(r"sqlite:///{}".format(PATH_TO_DB))
Base.metadata.create_all(bind=engine)
DBSession = sessionmaker(bind=engine)

session = DBSession()

d = [{'long': None, 'tweetid': None, 'timestamp': None, 'lat': None},
     {'userid': '113503286', 'long': '-87.624387',
      'tweetid': '595084326489956352',
      'timestamp': '1430714134224', 'lat': '41.852653'},
     {'userid': '2421024865', 'long': '-87.629798',
      'tweetid': '595084376632729600', 'timestamp': '1430714146179',
      'lat': '41.878114'}]

tweets = []
for tweetinfo in d:
    try:
        tweet = Tweet(**tweetinfo)
    except TypeError:
        continue
    else:
        tweets.append(tweet)

for tweet in tweets:
    session.add(tweet)
    try:
        session.commit()
    except IntegrityError:
        session.rollback()

答案 3 :(得分:0)

最好的方式,我如何理解你的问题:

dicts = [{'long': None, 'tweetid': None, 'timestamp': None, 'lat': None},
 {'userid': None, 'long': '-87.624387', 'tweetid': '595084326489956352', 'timestamp': '1430714134224', 'lat': '41.852653'}, 
 {'userid': '2421024865', 'long': '-87.629798', 'tweetid': '595084376632729600', 'timestamp': '1430714146179', 'lat': '41.878114'}]
print dicts

dicts = [d for d in dicts if "userid" in d and not d["userid"] == None]
print dicts

编辑:感谢评论

删除了d.keys()