Assitance in List Value Extractions - python

what I need right now is to extract a specific file
example:
List = [1,2,3,4,5,6]
print(List[1:2])
Output being [2]
what I need is exactly 2, and not [2]
To python in general, anything will help thanks

In [1]: List = [1,2,3,4,5,6]
In [2]: print(List[1])
2

When you are slicing an array OR list, it means you giving a range of index like [1:5] so, it must return a list. Better you can use single index of array OR list. Then it will return single instance.

Well the easiest way to do this is to just print the value of what you want. I.E:
list = [1,2,3,4,5,6,7,8,9]
print(list[1])
This will print out 2. I am confused on why you are looking in a range of the list which will output another list hence why you are getting [2]. I recommend going back and reading up on lists and what you can do to them in the python tutorials. Youtube might be a great help if your more visual as well. Best of luck
P.S. Remember that lists are 0 based and start at 0. Thus to get 2 in this list, you need to print out the 1 spot which is really the second spot in the list. 0 = 1, 1 = 2, 2 = 3, etc if you look at the list I created.

Related

Is it possible to split the elements of a list?

>>> a = bpy.context.selected_objects
>>> a[:2]
[bpy.data.objects['Sphere.001'], bpy.data.objects['Sphere.010']]
>>>
Two list results.
what i need is
It is to move the number after Sphere to notepad.
I do not know.
001 and 010
thank you.
Is this what you want?
for a in bpy.context.selected_objects:
print(a.name.split(".")[-1])
In Blender, you can just split the object's name on the '.' and take the last element in the resulting list. That should print out all those numbers that you want to copy.
#Matti is probably giving you good information on Blender, and the loop will work, but the most direct route to the general case of getting a modified version of each item in a list is by using a list comprehension.
x = ["Sphere.001", "Sphere.010"]
print([y.split(".")[-1] for y in x])

Searching index of a range of values in an array using python

I am using the following code to find the indices
result1=np.where(np.logical_and(R[i,:]>= 30, R[i,:]<= 30.3))
The output is
result1 = (array([284, 285]),)
When I run len(result1) I get a value of one.
Why is the length one when there are 2 elements.
The two numbers are indexes. I need to use the indexes but cannot because len(result1) is one.
I tried changing it to list but that did not help.
Please help me with this.
result1 is a tuple containing a single array, and thus has a length of 1; it is the array that has 2 elements.
You can use like len(result1[0]) or if you can remember later use like this; result1 = ([284,285],'') -> len(result1).

Deleting items that have the place swapped around in a returned list

My function takes a number, and a list of numbers.
If 2 numbers in the list add up to the original number, in the form [Num1, Num2].
Now I don't want any "duplicates" i.e. I only want [4, -7] returned and not [4, -7], [-7, 4].
def pairs(n, num_list):
newest_list = []
for j in range(len(num_list)):
for i in range(len(num_list)-1):
if num_list[j] + num_list[i+1] == n:
newest_list.append([num_list[j], num_list[i+1]])
return newest_list
Now I'd like a hint rather than code posted, a simple.
My question is:
Do I have the ability to do that within my code, and if so, a hint would be great, or will I need to define another function to do that for me?
You definitely have the ability to do that within your code.
A hint to complete this would be to think about at what point in your code it makes sense to stop searching for further matches and to return what you've found. Let me know if that's too cryptic!
You can still do that in your current code by simply appending these two numbers into a Set. For more info, this will help you.
if you have 2 lists l1, and l2 where:
l1=[1,2]
l2=[2,1]
If you convert them to sets, you can compare them and they will evaluate to True if they have the same elements, no matter what the order is:
set(l1) == set(l2) # this evaluates to True
In your if condition, before appending the numbers, you can check if the set set([num_list[j], num_list[i+1]]) is already in newest_list.
I am tempted to write some code, but you said not to, so I'll leave it here :p
You can leave your code the way it is, but before you return the list, you can filter the list with a predicate that the pair [a,b] is only accepted if pair [b,a] is not in the list
When adding a pair [a, b] to the result list, sort the pair, then see if it's in the result list. If so, don't add it.
Also, consider using a Python set.

python list modification to list of lists

I am trying to learn python (just finished Learn Python the Hard Way book!), but I seem to be struggling a bit with lists. Specifically speaking, I have a list like so:
x = ["/2.ext", "/4.ext", "/5.ext", "/1.ext"]
I would like to operate on this above list, so that it returns a list (somehow!) like so:
y = [ ["/1.ext", "/2.ext"], ["/1.ext", "/2.ext", "/3.ext, "/4.ext"], ["/1.ext", "/2.ext", "/3.ext", "/4.ext", "/5.ext"], ["/1.ext"] ]
So, essentially, each element in x is now turned to a list of lists. I could probably loop over x, store all the sequence lists in another list and then merge then together - but it just seems like there must be a better way to do it.
Would be grateful if someone could point me in the right direction to solve this problem.
EDIT (taking into account Martijn's comments):
Specifically, I want to generate the intermediary filenames in a sequence, ending at the number for each x list element
You can do it as follows:
x = ["/2.ext", "/4.ext", "/5.ext", "/1.ext"]
print [['/{}.ext'.format(j) for j in range(1,int(i[1])+1)] for i in x]
[OUTPUT]
[['/1.ext', '/2.ext'], ['/1.ext', '/2.ext', '/3.ext', '/4.ext'], ['/1.ext', '/2.ext', '/3.ext', '/4.ext', '/5.ext'], ['/1.ext']]
This only works for digits upto 9. I'll post update for more general solutions
HERE is the more general solution. Works for any numbers:
import re
x = ["/2.ext", "/4.ext", "/5.ext", "/1.ext"]
print [['/{}.ext'.format(j) for j in range(1,int(re.search(r'\d+',i).group(0))+1)] for i in x]

how can i append in reverse? python

.append
Function adds elements to the list.
How can I add elements to the list? In reverse? So that index zero is new value, and the old values move up in index?
What append does
[a,b,c,d,e]
what I would like.
[e,d,c,b,a]
Thank you very much.
Suppose you have a list a, a = [1, 2, 3]
Now suppose you wonder what kinds of things you can do to that list:
dir(a)
Hmmmm... wonder what this insert thingy does...
help(a.insert)
Insert object before index, you say? Why, that sounds a lot like what I want to do! If I want to insert something at the beginning of the list, that would be before index 0. What object do I want to insert? Let's try 7...
a.insert(0, 7)
print a
Well, look at that 7 right at the front of the list!
TL;DR: dir() will let you see what's available, help() will show you how it works, and then you can play around with it and see what it does, or Google up some documentation since you now know what the feature you want is called.
It would be more efficient to use a deque(double-ended queue) for this. Inserting at index 0 is extremely costly in lists since each element must be shifted over which requires O(N) running time, in a deque the same operation is O(1).
>>> from collections import deque
>>> x = deque()
>>> x.appendleft('a')
>>> x.appendleft('b')
>>> x
deque(['b', 'a'])
Use somelist.insert(0, item) to place item at the beginning of somelist, shifting all other elements down. Note that for large lists this is a very expensive operation. Consider using deque instead if you will be adding items to or removing items from both ends of the sequence.
Using Python's list insert command with 0 for the position value will insert the value at the head of the list, thus inserting in reverse order:
your_list.insert(0, new_item)
You can do
your_list=['New item!!']+your_list
But the insert method works as well.
lst=["a","b","c","d","e","f"]
lst_rev=[]
lst_rev.append(lst[::-1])
print(lst_rev)
Here's an example of how to add elements in a list in reverse order:
liste1 = [1,2,3,4,5]
liste2 = list()
for i in liste1:
liste2.insert(0,i)
Use the following (assuming x is what you want to prepend):
your_list = [x] + your_list
or:
your_list.insert(0, x)

Categories

Resources