How to map one list and dictionary in python [closed] - python

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
List output of program
print ('list', L)
['A','B']
dictionary output of program after the manipulation of program is below
print ('dictionary', d)
dictionary {'a':1,'b':2}
dictionary {'a':1,'b':2}
Expected Out
{'A':{'a':1,'b':2}, 'B': {'a':1,'b':2}}

list_out = ['A', 'B']
dict_out1 = {'a':1,,'b':2}
dict_out2 = {'a':1,,'b':2}
exp_out = {list_out[0]: dict_out1,
list_out[1]: dict_out2}

Related

sum integer part of the alphanumeric list in python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 days ago.
Improve this question
I have a list like below
l1 = ['a10','b2','a2','c1','b4','c5']
and I want to sum of numeric and output like below
l2 = ['a12','b6','c7'].
Note: do not use in-built function
from collections import Counter
l1 = ['a10','b2','a2','c1','b4','c5']
l2 = [f'{k}{v}' for (k, v) in Counter(''.join(i[0]*int(i[1:]) for i in l1)).items()]
# ['a12', 'b6', 'c6']
Figuring this out is left as an exercise to the reader.
It also assumes the alpha part is always just the first character.

zip list with more than 2 values to dict python [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have this two lists:
values = ['0.7%', '11.1%', '8.7%']
key = ['NA']
which I want to zip to dict(ie. Keys and value). so that output will look like this:
{'NA': '0.7%', '11.1%', '8.7%'}
here is my current code:
values = ['0.7%', '11.1%', '8.7%']
key = ['NA']
joiner = dict(zip(key, values))
print(joiner)
currrent output:
{'NA': '0.7%'}
Completed:
values = ['0.7%', '11.1%', '8.7%']
key = ['NA']
joiner = {f"{key[0]}": values}
print(joiner)
print({key[0]:' ,'.join(values)})

Sorting a list in python based on the last number in each string entry [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I have a list in python of the following form:
myList = ['r0x94', 'r0x21', 'r0x51']
I want to sort it based on the last number in each string entry of the list such that:
sorted_myList = ['r0x21', 'r0x51', 'r0x94']
The last number is not hex, rather it is decimal. How to do it?
>>> my_list = ['r0x94', 'r0x21', 'r0x51']
>>> sorted(my_list, key=lambda x: int(x.rpartition('x')[-1]))
['r0x21', 'r0x51', 'r0x94']

How to change values in a list - python? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
If I have a list like:
['15', 'A:B', 'B:C', 'D:C', 'F:A']
I want to change the values like:
['1515', 'A:B15', 'B:C15', 'D:C15', 'F:A15']
(add first one to other values in a list)
Is it possible?
Assuming ["15", "A:B", "B:C", "D:C", "F:A"] is your input list, a simple list comprehension would do the job:
>>> l = ["15", "A:B", "B:C", "D:C", "F:A"]
>>> value_to_add = l[0]
>>> [item + value_to_add for item in l]
['1515', 'A:B15', 'B:C15', 'D:C15', 'F:A15']

converting a dictionary into 2 lists [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm trying to convert a dictionary
clues={#:A,+:B,6:C}
into
clues1=[#,+,6]
clues2=[A,B,C]
when I use clues.values it doesn't let me iterate through it and any other method I have used has given an error message
Do you want to set keys to clues1 and values to clues2?
clues1 = list(clues.keys())
clues2 = list(clues.values())
If you want the lists to be in alphabetical order, but still have the indicies match, you can do:
clues={'#':'A','+':'B','6':'C'}
cluesKeys = list(clues.iterkeys())
cluesValues = list(clues.itervalues())
If you explicitly want an iteration over the "clues" dictionary:
clues={'#':'A','+':'B','6':'C'}
clues1=list()
clues2=list()
for keys, values in clues.items():
clues1.append(keys)
clues2.append(values)
print clues1
print clues2
['#', '+', '6']
['A', 'B', 'C']

Categories

Resources