简体   繁体   English

如何在dict中动态追加到数组?

[英]How to dynamically append to array in dict?

This has taken me over a day of trial and error. 这让我花了整天的反复试验。 I am trying to keep a dictionary of queries and their respective matches in a search. 我正在尝试在搜索中保留查询字典及其各自的匹配项。 My problem is that there can be one or more matches. 我的问题是可能有一个或多个匹配项。 My current solution is: 我当前的解决方案是:

match5[query_site] will already have the first match but if it finds another match it will append it using the code below. match5[query_site]将已经有第一个匹配项,但是如果找到另一个匹配项,它将使用下面的代码将其追加。

temp5=[]  #temporary variable to create array              
if isinstance(match5[query_site],list):  #check if already a list              
    temp5.extend(match5[query_site])
    temp5.append(match_site)
else:
    temp5.append(match5[query_site])
match5[query_site]=temp5 #add new location

That if statement is literally to prevent extend converting my str element into an array of letters. 该if语句实际上是为了防止扩展将我的str元素转换为字母数组。 If I try to initialize the first match as a single element array I get None if I try to directly append. 如果我尝试将第一个匹配项初始化为单个元素数组,并且尝试直接附加,则会得到None I feel like there should be a more pythonic method to achieve this without a temporary variable and conditional statement. 我觉得应该有更多的pythonic方法来实现此目的,而无需使用临时变量和条件语句。

Update: Here is an example of my output when it works 更新:这是我的输出正常工作的示例

5'flank: ['8_73793824', '6_133347883', '4_167491131', '18_535703', '14_48370386']
3'flank: X_11731384

There's 5 matches for my "5'flank" and only 1 match for my "3'flank". 我的“ 5'侧翼”有5个比赛,而我的“ 3'侧翼”只有1个比赛。

I like using setdefault() for cases like this. 我喜欢在这种情况下使用setdefault()

temp5 = match5.setdefault(query_site, [])
temp5.append(match_site) 

It's sort of like get() in that it returns an existing value if the key exists but you can provide a default value. 有点类似于get() ,如果键存在,它将返回一个现有值,但是您可以提供一个默认值。 The difference is that if the key doesn't exist already setdefault inserts the default value into the dict. 区别在于,如果键不存在,则setdefault将默认值插入dict中。

So what about this: 那么呢:

if query_site not in match5:  # here for the first time
   match5[query_site] = [match_site]
elif isinstance(match5[query_site], str):  # was already here, a single occurrence
    match5[query_site] = [match5[query_site], match_site]  # make it a list of strings
else:  # already a list, so just append
   match5[query_site].append(match_site)

This is all you need to do 这就是你要做的

if query_site not in match5:
    match5[query_site] = []
temp5 = match5[query_site]
temp5.append(match_site)

You could also do 你也可以

temp5 = match5.setdefault(query_site, [])
temp5.append(match_site)

Assuming match5 is a dictionary, what about this: 假设match5是一个字典,那么呢:

if query_site not in match5:  # first match ever
   match5[query_site] = [match_site]
else:  # entry already there, just append
   match5[query_site].append(temp5)

Make the entries of the dictionary to be always a list, and just append to it. 使字典的条目始终为列表,然后追加到列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM