简体   繁体   中英

Joining value(list) from <key, value> in dictionary

I have a dictionary in format {'string-value-keys':[list-values]}. For a particular key value, I would like to retrieve the value as a string. Here is the example:

>>> simpleDict= {'DDL_TABLE1':['create table bla', ' columns bla bla'], 'DML_TABLE1': ['insert into bla', ' values bla']}
>>> simpleDict
{'DDL_TABLE1': ['create table bla', ' columns bla bla'], 'DML_TABLE1': ['insert into bla', ' values bla']}
>>> sqlQry= " ".join(value for key, value in simpleDict.items() if 'DDL' in key)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, list found
>>> sqlQry= " ".join(value for key, value in simpleDict.items() if 'DDL' in key)

I am not able to understand why I am getting this error, when value is a list of string values.

Move the join inside:

(' '.join(value) for key, value in simpleDict.items() if 'DDL' in key)

Test:

>>> simpleDict= {'DDL_TABLE1':['create table bla', ' columns bla bla'], 'DML_TABLE1': ['insert into bla', ' values bla']}
>>> list(' '.join(value) for key, value in simpleDict.items() if 'DDL' in key)
['create table bla  columns bla bla']

Edit based on comment

This is easier to look at with a list comprehension vs a generator.

Your original as an LC:

>>> [value for key, value in simpleDict.items() if 'DDL' in key]
[['create table bla', ' columns bla bla']]

Which is creating a List of List of strings. If you try and use join on that:

>>> ' '.join([value for key, value in simpleDict.items() if 'DDL' in key])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: sequence item 0: expected string, list found

produces the error you saw.

OK, depending on what your full data looks likes, you can do two joins:

>>> ' '.join([' '.join(value) for key, value in simpleDict.items() if 'DDL' in key])

If you just want one big string, multiple joins isn't the end of the world.

If you are only looking for one item, you can do this:

>>> [' '.join(value) for key, value in simpleDict.items() if 'DDL' in key][0]
'create table bla  columns bla bla'

If you are dealing with multiple hits in the data/multiple uses, use a loop:

for s in [' '.join(value) for key, value in simpleDict.items() if 'DDL' in key]:
   # s multiple times

If the data is 'needle in a haystack' type, use a loop and break out once you find what you are looking for.

" ".join() expects an iterable of strings and your generator yields lists of strings, so you are ending up with a call like " ".join([['create table bla', ' columns bla bla']]) .

Since it looks like you only expect a single key to match, you probably don't want a generator for this. I would suggest the following:

sqlQry = None
for key, value in simpleDict.items():
    if 'DDL' in key:
        sqlQry = " ".join(value)
        break

Perhaps you wanted to join the results of the value:

In [11]: sqlQry= " ".join(''.join(value) for key, value in simpleDict.items() if 'DDL' in key)

In [12]: sqlQry
Out[12]: 'create table bla columns bla bla'

at the moment you are trying to join a list of lists..

In [13]: ''.join([['a','b']])
TypeError                                 Traceback (most recent call last)
<ipython-input-13-7289bd6c8196> in <module>()
----> 1 ''.join([['a','b']])

TypeError: sequence item 0: expected string, list found

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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