converting a dictionary into 2 lists [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 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']

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.

How to map one list and dictionary in 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 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}

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']

Reformat Python's vector list results to a single comma delimitted one [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
How can I reformat the results from a vector array (Python) to a single comma delimitted one?
I have these results:
[ ['44231#0:', '2016/10/11', '11:19:23', '11:19:23', '1', '11:19:24', '11:19:24'
, '0']
['44231#0:', '2016/10/11', '12:20:39', '12:20:58', '12:20:59', '12:20:59', '0']
]
And I need something like:
44231#0:, 2016/10/11, 11:19:23, 11:19:23, 1, 11:19:24, 11:19:24
44231#0:, 2016/10/11, 12:20:39, 12:20:58, 12:20:59, 12:20:59, 0
Anyone?
thank you in advance!!
the simply way will be using two for loop.
for d in data:
s = ''
for l in d:
s += l + ', '
print(s[:-2])
Are you trying to make a list of strings? Do:
[', '.join(sublist) for sublist in data]

Concatenate a list of lists into a string printed on separate lines [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 have a list of lists of strings such that:
decipher = [['###>>?', '######', '*&*...', '##&#&#'], ['###>>?', '######', '*&*...', '######'], ['###>>?', '######', '*&*...', '######'], ['###>>?', '######', '*&*...', '######']]
I need to concatenate items with the same indeces from each list to print on the same line, each subsequent set of identically indexed items have to print on a new line:
###>>?###>>?###>>?###>>?
########################
*&*...*&*...*&*...*&*...
##&#&###&#&###&#&###&#&#
How can I accomplish that? Thanks!
A simple one line solution would be:
>>> print '\n'.join(''.join(i) for i in zip(*decipher))
###>>?###>>?###>>?###>>?
########################
*&*...*&*...*&*...*&*...
##&#&###################
Here is another solution:
for col in range(len(decipher[0])):
out = ''
for row in range(len(decipher)):
out += decipher[row][col]
print(out)
###>>?###>>?###>>?###>>?
########################
*&*...*&*...*&*...*&*...
##&#&###################

Categories

Resources