简体   繁体   English

如何在一些附加条件下将值插入python中的列表

[英]how to insert values into a list in python.. with a few extra conditions

hi i have a list of tuples like this: 嗨,我有一个这样的元组列表:

bigrams = [ ('wealth', 'gain'), ('gain', 'burnt'), ('burnt', 'will'), ('will', 'fire') ] bigrams = [('wealth','gain'),('gain','burnd'),('burnd','will'),('will','fire')]]

and i wish to append each individual tuple to a dictionary as the dictionarys key. 我希望将每个元组附加到字典作为字典的键。

i want the format to look like this. 我希望格式看起来像这样。

dict = {"wealth-gain": value, "gain-burnt": value ......} 

how would i create a loop that will go through each tuple in the bigrams list and append it to the dictionary? 我将如何创建一个循环,该循环将遍历bigrams列表中的每个元组并将其附加到字典中?

Heres what i have 这是我所拥有的

For word in bigrams:
    dict[(0+"-"+1) = dict

basicaly i want to take each tuple and add a "-" in between each word in the tuple then append that to a dictionary? 基本上我想带每个元组并在元组中的每个单词之间添加一个“-”,然后将其附加到字典中?

Any ideas how to do this? 任何想法如何做到这一点?

Also if the bigram that is going to be appended to the dictionary matches a bigram that is already in the dictionary i would not like to add that bigram to the dictionary. 同样,如果将要添加到字典中的双字母组与字典中已经存在的双字母组匹配,我也不想将该双字母组添加到字典中。 Rather i would like to increment the value of the bigram already in the dictionary. 而是我想增加字典中已经存在的二元组的值。 Any ideas how to do that? 任何想法如何做到这一点?

Thanks. 谢谢。

You can use the join method: 您可以使用join方法:

bigrams = [ ('wealth', 'gain'), ('gain', 'burnt'), ('burnt', 'will'), ('will', 'fire') ]
dict_ = {}
for tup in bigrams:
    k = '-'.join(tup)
    dict_[k] = data.setdefault(k,0) + 1

or express the initialization with a generator: 或用生成器表达初始化:

bigrams = [ ('wealth', 'gain'), ('gain', 'burnt'), ('burnt', 'will'), ('will', 'fire') ]
dict_ = dict(('-'.join(tup), 0) for tup in bigrams)

How about: 怎么样:

d = {}
val = 0
bigrams = [ ('wealth', 'gain'), ('gain', 'burnt'), ('burnt', 'will'), ('will', 'fire') ]
for word in bigrams:
    s = '-'.join(word)
    if s in d:
        d[s] += 1
    else:
        d[s] = val

You could use the tuples in the list directly as dictionary keys -- you do not need to join them to a single string. 您可以将列表中的元组直接用作字典键-无需将它们连接到单个字符串。 In Python 2.7, this gets particularly convenient in combination with collections.Counter : 在Python 2.7中,与collections.Counter结合使用时特别方便。

from collections import Counter
bigrams = [('wealth', 'gain'), ('gain', 'burnt'),
           ('burnt', 'will'), ('will', 'fire')]
counts = Counter(bigrams)
print counts

prints 版画

Counter({('gain', 'burnt'): 1, ('will', 'fire'): 1, ('wealth', 'gain'): 1, ('burnt', 'will'): 1})

You should make yourself familiar with list comprehension. 您应该使自己熟悉列表理解。 That makes the transformation of the first list easier: 这使得第一个列表的转换更加容易:

bigrams = [x+"-"+y for x,y in bigrams]

Now have a look at the setdefault method of dict and use it like this: 现在看看dict的setdefault方法,并像这样使用它:

data = {} # dict is build in, so don't use it as variable name data = {}#dict是内置的,因此请勿将其用作变量名

for bigram in bigrams:
    data[bigram] = data.setdefault(bigram,0) + 1

If you want to have an even more compressed version, you might look at the itertools module. 如果您想要一个更压缩的版本,可以查看itertools模块。

try this: 尝试这个:

bigrams = [ ('wealth', 'gain'), ('gain', 'burnt'), ('burnt', 'will'), ('will', 'fire') ]
bigram_dict = {}
for bigram in bigrams:
    key = '-'.join(bigram)
    if key in bigram_dict:
        bigram_dict[key] += 1
    else:
        bigram_dict[key] = 1

also, I feel obliged to point out that these are not bigrams in any way, shape or form. 另外,我有义务指出,这些在任何形式,形状或形式上都不是二元组。 I'm not sure exactly what you mean to say, but they're certainly not bigrams! 我不确定您要说的是什么意思,但它们当然不是最重要的!

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

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