简体   繁体   English

将列表值与字典的公共键结合起来

[英]combine list values with common keys to a dictionary

I have the a list like我有一个清单

{ 'person1', 'Hi', 'person1', 'You there?', 'person2', 'Hello', 'person1', 'Hru?', 'person2'','Good' } { 'person1', '嗨', 'person1', '你在吗?', 'person2', '你好', 'person1', 'Hru?', 'person2'', 'Good' }

How do i convert this into a dictionary by grouping keys and combining text values?我如何通过分组键和组合文本值将其转换成字典? Expected result:预期结果:

{'person1': 'Hi You there? {'person1':'嗨,你好吗? Hru?', 'person2': 'Hello Good'} Hru?', 'person2': '你好'}

NOTE: The grouping should be done using the key names.注意:应该使用键名进行分组。 As the conversation isn't always person1 followed by person2.由于对话并不总是 person1 后跟 person2。

The code from aniketh seems to work.来自 aniketh 的代码似乎有效。 I now need to apply this on a dataframe which has many of this lists.我现在需要在 dataframe 上应用它,它有很多这样的列表。

      chatid   list_of_convo
--------------------------------------------------------------------
0     12         {'person1', 'Hi','person1', 'You there?','person2', 
                  'Hello','person1', 'Hru?','person2','Good'}
1     21          {'person3', 'Hi','person3', 'You there?','person2', 
                   'Hello','person3', 'Hru?','person2','Good'}
2     34          {'person5', 'Hi','person5', 'You there?','person2', 
                   'Hello','person5', 'Hru?','person2','Good'}

The answer to above is:上面的答案是:

def Convert(lst):
    dct = {}
    for ind in range(len(lst)):
      if ind % 2 == 0:
        if lst[ind] not in dct:
          dct[lst[ind]] = ""

    for ind in range(len(lst)):
      if ind % 2 == 1:
        dct[lst[ind - 1]] += lst[ind] + " "

    return dct

df['grouped'] = df['list_of_convo'].apply(Convert)

(For other's reference) (供他人参考)

The following code could work:以下代码可以工作:

lst = [ 'person1:', 'Hi', 'person2:', 'Hello', 'person1:', 'Hru?', 'person2:','Good' ]

dct = {}
for ind in range(len(lst)):
  if ind % 2 == 0:
    if lst[ind] not in dct:
      dct[lst[ind]] = ""

for ind in range(len(lst)):
  if ind % 2 == 1:
    dct[lst[ind - 1]] += lst[ind] + " "

print(dct)

Output: Output:

{'person1:': 'Hi Hru? ', 'person2:': 'Hello Good '}

First, to initialize a list, we use [] instead of {}首先,要初始化列表,我们使用[]而不是{}

After we make our dictionary, dct , we can iterate through every even indexed element of lst to get the elements ( 'people1' , 'people2' ) that will be the keys of dct .在创建字典dct之后,我们可以遍历lst的每个偶数索引元素以获取将成为dct键的元素( 'people1''people2' )。 First, we check if the key already exists in dct , and if not, we add it in.首先,我们检查密钥是否已经存在于dct中,如果不存在,我们将其添加进去。

Finally, we iterate through the odd indexed elements and concatenate them to the values of their respective keys (the element before them in lst ).最后,我们遍历奇数索引元素并将它们连接到它们各自键的值( lst中它们之前的元素)。

I hope this helped!我希望这有帮助! Please let me know if you need any further help or clarification!如果您需要任何进一步的帮助或说明,请告诉我!

Note: If you care about the extra space in the output, add the following code segment before you print to get rid of the extra space:注意:如果您关心 output 中的多余空间,请在打印之前添加以下代码段以去除多余空间:

for key, value in dct.items():
  dct[key] = value[:-1]

Second Input:第二个输入:

lst = ['person1', 'Hi', 'person1', 'You there?', 'person2', 'Hello', 'person1', 'Hru?', 'person2','Good' ]

Output: Output:

{'person1': 'Hi You there? Hru?', 'person2': 'Hello Good'}

Try:尝试:

pairs = zip(my_list[::2], my_list[1::2])
mydict = {}

for k, v in pairs:
   mydict[k] = (mydict.get(k, '') + ' ' + v).strip()

my_list[::2] will get every other element of the list starting at the first element (which should be your keys). my_list[::2] 将从第一个元素(应该是您的键)开始获取列表中的所有其他元素。 my_list[1::2] will do the same but starting on the second element and this should be your values. my_list[1::2] 会做同样的事情,但从第二个元素开始,这应该是你的价值观。 zip() will make a list of tuples that pair each key and value. zip() 将生成一个元组列表,将每个键和值配对。 Then the last line just parses that list of key-value tuples into an actual dict based on your requirements around repeat keys.然后最后一行只是根据您对重复键的要求将该键值元组列表解析为实际的字典。

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

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