Getting values from a dict in 2nd field [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 7 years ago.
Improve this question
For example, I have this:
alphabetValues = {"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7...
Is it possible if instead of having:
print(alphabetValues["c"])
To having something that would get "e" if I searched for 5 in a dict.
"e":5
Thanks in advance.

As suggested by jonrsharpe, you need to reverse your dictionnary :
alphabetValues = {"a":1,"b":2,"c":3,"d":4,"e":5,"f":6,"g":7}
revalpha={v:k for k,v in alphabetValues.iteritems()}
>>> revalpha[5]
'e'

Why not set up an alphabet list?
alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']
print(alphabet[0]) #will print out 'a'
print(alphabet[25]) #will print out 'z'
Note that all values are 1 less than expected.

Related

How to use strings as an ordering sequence? [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'd like to know how I can use a string like order = '8927391' (with 8 being largest and 1 being smallest) and print out an answer according to the order, for example:
Let's say if_no_is_higher(no1, no2) is a function that I have defined already to print yes if the first number is higher than the second, and no if the first number is smaller.
if_no_is_higher(8, 9)
returns
yes
because according to the order 8 is higher than 9
Create a custom order and use the index of that collection to check for your condition:
a = '8927391'
custom_order = [int(item) for item in a]
min(8, 9, key=custom_order.index)

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 do I convert string to 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 years ago.
Improve this question
How do I convert def stringParametre(x) x to a list so that if x is hello then it will become a list like ["H","E","l","l","O"] . Capitals don't matter .
Note that you do not have to convert to a list if all you want to do is to iterate over the characters of the string:
for c in "hello":
# do something with c
works
Building on #idjaw's comment:
def stringParametre(x):
return list(x)
Of course this will have an error if x is not a string (or other sequence type).
list(x)
OR
mylist = []
for c in x:
mylist.append(c)

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)
###>>?###>>?###>>?###>>?
########################
*&*...*&*...*&*...*&*...
##&#&###################

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