辞書に入れたいリストがあります。
list_x = ['a', 'ada', 'aadsad', ......, 'sd']
このリストにあるものは何でも、リストの 2 番目の項目がキーになり、位置 0 を含む他のすべてが値になるように辞書になるには (今のところ 13 としましょう)。
他の例の dict() 関数を試してみましたが、望みどおりの結果が得られません。前もって感謝します。
辞書に入れたいリストがあります。
list_x = ['a', 'ada', 'aadsad', ......, 'sd']
このリストにあるものは何でも、リストの 2 番目の項目がキーになり、位置 0 を含む他のすべてが値になるように辞書になるには (今のところ 13 としましょう)。
他の例の dict() 関数を試してみましたが、望みどおりの結果が得られません。前もって感謝します。
If you don't want to modify your list
:
In [13]: list_x = ['a', 'ada', 'aadsad','sd']
In [14]: d={list_x[1]:list_x[0:1]+list_x[2:]}
In [15]: d
Out[15]: {'ada': ['a', 'aadsad', 'sd']}
EDIT:
if you're iterating over multiple lists:
In [72]: dic={}
In [73]: for x in multiple_lists:
....: dic[x[1]]=x[0:1]+x[2:]
....:
....:
これはうまくいきませんか?
d = {}
d[list_x.pop(1)] = list_x
オリジナルを変更せずに
tmp = list_x[:]
{tmp.pop(1): tmp}
A strange requirement, but OK.
dict_x = {list_x.pop(1): list_x}
そして今、元のリストを変更せず、スライスを追加しない答え:
>>> r = range(len(list_x))
>>> del r[1]
>>> {list_x[1]: operator.itemgetter(*r)(list_x)}
{'ada': ('a', 'aadsad', ......, 'sd')}
そして、言及されていないスライスなしの他の方法:
dict.fromkeys( (list_x.pop(1),), list_x)