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)
Related
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 days ago.
Improve this question
I want to give the values in all_X_H in there normal indices order a to variables in ring[corrector_indexes[m]].KickAngle which doesn't have the same order as a.
This is a simple example:
all_X_H = [
0,-0.000009
1,-0.000018
2,-0.000010
3,-0.000007]
corrector_indexes = [1,2,3,4]
final_corr_order_H = [3,2,1,0]
for m in final_corr_order_H:
for a in range(len(all_X_H)):
ring[corrector_indexes[m]].KickAngle = [all_X_H[a],0]
The result i want to see is
ring[corrector_indexes[3]].KickAngle = [all_X_H[0],0]
ring[corrector_indexes[2]].KickAngle = [all_X_H[1],0]
ring[corrector_indexes[1]].KickAngle = [all_X_H[2],0]
ring[corrector_indexes[0]].KickAngle = [all_X_H[3],0]
How can i define two different indices in one for loop to implement this?
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
I am trying to write a code that picks values in from a list to create to separate lists.
This is my code so far;
list = [6,2,9,10]
for x, y in zip(list, list):
print (x,y)
Output:
6,2
9,10
But what I want is:
[6,9]
[2,10]
You can zip slices of your original list to achieve your goal:
list = [6,2,9,10]
for x, y in zip(list[:2], list[2:]):
print (x,y)
6 9
2 10
This is not well-generalized - if you had a specific intent like "split my list into a first-half and second-half, and match the elements of those sub-lists" that would be more clear, but hopefully this helps you achieve what you want.
Assuming you want the elements at even index and odd index split into separate lists, you could do the following.
List1 =[1, 2,3,4,5]
even_list= List1[::2]
Odd_list = List1[1::2]
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 4 years ago.
Improve this question
I want to transform a slice in some index so I can access each element of a vector at a time.
How can I do that?
You can use the indices method of the slice to generate a tuple that can be passed to range. You need to know the length of your instance though.
For example:
>>> sl = slice(2) # or wherever you get that from
>>> length = 100
>>> list(range(*sl.indices(length)))
[0, 1]
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']
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.