Python - 将列表添加到dict(初学者) [英] Python--adding list into dict (beginner)
问题描述
我非常新的编程(现在我的第一堂课),所以承担我的格式问题和误会,或错过简单的修复。
我有一个带有推文数据的dict:'user'作为键,然后是'text'作为它们的值。我的目标是找到他们回复另一个用户的推文,以@符号开头,然后再创建一个包含作者的用户和他回复的所有用户的新的dict。这是我以下相当简单的if语句。我也可以使用split函数隔离他们正在回复的人的用户名(该函数将@符号和之后的下一个空格中的所有文本)。
$ b $ a,b,c,d,e中的t为
en =''
task1dict = {}
,f,g,h,i,j,k,l,m,n:
如果t ['text'] [0] =='@':
user = t ['user' ]
repliedto = t ['text']。split(st)[ - 1] .split(en)[0]
task1dict [user] = [repliedto]
Username1回复到username2。用户名2回复了username3和username5。
我正在尝试创建一个可以读取以下内容的dict(caled tweets1):
'user':'repliedto'
username1:[username2]
username2:[username3,username5]
等。
有没有更好的方法隔离用户名,然后把它们成为一个新的字典?以下是tweet数据的2个条目示例:
{user:datageek88,text:@ sundevil1992好的问题!@joeclarknet这是考试吗?},
{user:joeclarkphd,文本:考试问题将在适当的时候回答@ sundevil1992}
我现在可以将其添加到一个dict,但它只会为每个用户保存一个repliedto,所以而不是显示username2已经回复了3和5,它只显示最新的一个,5:
{'username1' ['username2'],
'username2':['username5']}
如果我在这里做任何一个严肃的no-no,我很抱歉,请告诉我我在做错什么!
我可能会这样做。使用以下正则表达式来标识所有用户名。
r@([^ \s] *)
pre>
它意味着寻找
@
符号,然后返回所有不是空格的字符。一个defaultdict
是一个简单的字典,如果找不到键,则返回默认值。在这种情况下,在我们添加新密钥的情况下,我指定一个空的集合
作为返回类型。import re
from collections import defaultdict
tweets = [{user:datageek88,text:@ sundevil1992 good question!@joeclarknet Is这个考试?},
{user:joeclarkphd,text:考试问题将在适当时候回答@ sundevil1992}]
from_to = defaultdict (设置)
在tweet中的推文:
如果@in tweet ['text']:
user = tweet ['user']
for replied_to in re.findall (r@([^ \s] *),tweet ['text']):
from_to [user] .add(replied_to)
print from_to
输出
defaultdict(< type'list'> {'joeclarkphd':['sundevil1992'],
'datageek88':['sundevil1992','joeclarknet']})
I'm very new to programming (taking my first class in it now), so bear with me for format issues and misunderstandings, or missing easy fixes.
I have a dict with tweet data: 'user' as keys and then 'text' as their values. My goal here is to find the tweets where they are replying to another user, signified by starting with the @ symbol, and then make a new dict that contains the author's user and the users of everyone he replied to. That's the fairly simple if statement I have below. I was also able to use the split function to isolate the username of the person they are replying to (the function takes all the text between the @ symbol and the next space after it).
st='@' en=' ' task1dict={} for t in a,b,c,d,e,f,g,h,i,j,k,l,m,n: if t['text'][0]=='@': user=t['user'] repliedto=t['text'].split(st)[-1].split(en)[0] task1dict[user]=[repliedto]
Username1 replied to username2. Username2 replied to both username3 and username5.
I am trying to create a dict (caled tweets1) that reads something like:
'user':'repliedto' username1:[username2] username2:[username3, username5]
etc.
Is there a better way to isolate the usernames, and then put them into a new dict? Here's a 2 entry sample of the tweet data:
{"user":"datageek88","text":"@sundevil1992 good question! @joeclarknet Is this on the exam?"}, {"user":"joeclarkphd","text":"Exam questions will be answered in due time @sundevil1992"}
I am now able to add them to a dict, but it would only save one 'repliedto' for each 'user', so instead of showing username2 have replied to both 3 and 5, it just shows the latest one, 5:
{'username1': ['username2'], 'username2': ['username5']}
Again, if I'm making a serious no-no anywhere in here, I apologize, and please show me what I'm doing wrong!
解决方案I might do it like this. Use the following regular expression to identify all usernames.
r"@([^\s]*)"
It means look for the
@
symbol, and then return all characters that aren't a space. Adefaultdict
is a simply a dictionary that returns a default value if they key isn't found. In this case, I specify an emptyset
as the return type in the event that we are adding a new key.import re from collections import defaultdict tweets = [{"user":"datageek88","text":"@sundevil1992 good question! @joeclarknet Is this on the exam?"}, {"user":"joeclarkphd","text":"Exam questions will be answered in due time @sundevil1992"}] from_to = defaultdict(set) for tweet in tweets: if "@" in tweet['text']: user = tweet['user'] for replied_to in re.findall(r"@([^\s]*)", tweet['text']): from_to[user].add(replied_to) print from_to
Output
defaultdict(<type 'list'>, {'joeclarkphd': ['sundevil1992'], 'datageek88': ['sundevil1992', 'joeclarknet']})
这篇关于Python - 将列表添加到dict(初学者)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!