Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python——将列表添加到dict(初学者)_Python_List_Dictionary - Fatal编程技术网

Python——将列表添加到dict(初学者)

Python——将列表添加到dict(初学者),python,list,dictionary,Python,List,Dictionary,我对编程非常陌生(现在正在上我的第一堂课),所以请容忍我的格式问题和误解,或者错过了简单的解决方法 我有一个dict,tweet数据是:“user”作为键,然后“text”作为值。我的目标是找到他们回复其他用户的tweet,以@符号开头,然后制作一个新的dict,其中包含作者的用户和他回复的每个人的用户。这就是我下面所说的相当简单的if语句。我还能够使用split函数来隔离他们回复的人的用户名(该函数获取@符号和它后面的下一个空格之间的所有文本) Username1回复了username2。Us

我对编程非常陌生(现在正在上我的第一堂课),所以请容忍我的格式问题和误解,或者错过了简单的解决方法

我有一个dict,tweet数据是:“user”作为键,然后“text”作为值。我的目标是找到他们回复其他用户的tweet,以@符号开头,然后制作一个新的dict,其中包含作者的用户和他回复的每个人的用户。这就是我下面所说的相当简单的if语句。我还能够使用split函数来隔离他们回复的人的用户名(该函数获取@符号和它后面的下一个空格之间的所有文本)

Username1回复了username2。Username2同时回复username3和username5

我正在尝试创建一个dict(caled tweets1),内容如下:

'user':'repliedto'
username1:[username2]
username2:[username3, username5]
等等

有没有更好的方法来隔离用户名,然后将它们放入新的dict中?以下是tweet数据的2个条目示例:

{"user":"datageek88","text":"@sundevil1992 good question! @joeclarknet Is this on the exam?"},
{"user":"joeclarkphd","text":"Exam questions will be answered in due time @sundevil1992"}
我现在可以将它们添加到dict中,但它只会为每个“用户”保存一个“repliedto”,因此它不会显示用户名2已回复3和5,而是显示最新的用户名5:

{'username1': ['username2'],
'username2': ['username5']}

再说一次,如果我在这里的任何地方做出了严重的拒绝,我道歉,请告诉我我做错了什么

将最后一行修改为

 task1dict.setdefault(user, [])
 task1dict[user].append (repliedto)
每次编辑数组时,您都会覆盖用户回复的数组。如果dict不存在,setdefault方法会将其设置为空列表。然后只是附加到列表中

编辑:相同的代码使用一个唯一性集合

 task1dict.setdefault(user, set())
 task1dict[user].add (repliedto)

对于集合,可以向集合中添加元素。而您附加到列表中的列表

我可能会这样做。使用以下正则表达式标识所有用户名

r"@([^\s]*)"
这意味着查找
@
符号,然后返回所有非空格字符。
defaultdict
是一个简单的字典,如果找不到它们的键,它将返回默认值。在本例中,我指定一个空的
set
作为添加新键时的返回类型

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
输出
defaultdict(,{'joeclarkphd':['sundevil1992'],
'datageek88':['sundevil1992','joeclarknet']})

太棒了!它几乎完全符合我的要求。然而,有没有简单的方法让它忽略重复的内容?例如,username2在两个不同的时间回复username5,因此它在task1dict.Yes中显示username5两次。而不是使用列表。使用一套。集合类似于列表,但不能包含重复项。我将修改我的答案
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
defaultdict(<type 'list'>, {'joeclarkphd': ['sundevil1992'], 
'datageek88': ['sundevil1992', 'joeclarknet']})