The following code works:
image_files=['imgpy1.png','imgpy2.png']
clip = moviepy.video.io.ImageSequenceClip.ImageSequenceClip(image_files, fps=20)
But I have 200 filenames 'imgpy1.png', 'imgpy2.png' and so on. How do I create an object called image_files, that contain these file names. I tried to create an array of strings:
flist=[]
flist=list(map(str,flist))
flist.append['imagepy1.png']
flist.append['imagepy2.png']
but that does not work. I get the error message
Traceback (most recent call last):
File "C:\Users\vince\image.py", line 53, in <module>
flist.append['imagepy1.png']
TypeError: 'builtin_function_or_method' object is not subscriptable
I am new to Python and can't figure out how to get an array or Tuple of strings. I've tried for 2-3 hours to non avail, at least 40 different ways including stuff I read online that don't work on my laptop. In the end, I am only interested in passing an argument to the video function that (1) does not crash the Python interpreter and (2) can be digested by the video function. So far, I am stuck with (1). But in the end I'm looking at solving (2). I don't care if it's an array, a list, a Tuple or anything else, as long as it works.
I think append function is not correct.
flist.append('imagepy1.png') # not brackets
flist.append('imagepy2.png')
flist = []
for i in range(200):
flist.append(f"imagepy{i + 1}.png") #you use square brackets not round ones
print(flist)
this gets you
['imagepy1.png', 'imagepy2.png', 'imagepy3.png', 'imagepy4.png', 'imagepy5.png', 'imagepy6.png', 'imagepy7.png', 'imagepy8.png', 'imagepy9.png', 'imagepy10.png', 'imagepy11.png', 'imagepy12.png', 'imagepy13.png', 'imagepy14.png', 'imagepy15.png', 'imagepy16.png', 'imagepy17.png', 'imagepy18.png', 'imagepy19.png', 'imagepy20.png', 'imagepy21.png', 'imagepy22.png', 'imagepy23.png', 'imagepy24.png', 'imagepy25.png', 'imagepy26.png', 'imagepy27.png', 'imagepy28.png', 'imagepy29.png', 'imagepy30.png', 'imagepy31.png', 'imagepy32.png', 'imagepy33.png', 'imagepy34.png', 'imagepy35.png', 'imagepy36.png', 'imagepy37.png', 'imagepy38.png', 'imagepy39.png', 'imagepy40.png', 'imagepy41.png', 'imagepy42.png', 'imagepy43.png', 'imagepy44.png', 'imagepy45.png', 'imagepy46.png', 'imagepy47.png', 'imagepy48.png', 'imagepy49.png', 'imagepy50.png', 'imagepy51.png', 'imagepy52.png', 'imagepy53.png', 'imagepy54.png', 'imagepy55.png', 'imagepy56.png', 'imagepy57.png', 'imagepy58.png', 'imagepy59.png', 'imagepy60.png', 'imagepy61.png', 'imagepy62.png', 'imagepy63.png', 'imagepy64.png', 'imagepy65.png', 'imagepy66.png', 'imagepy67.png', 'imagepy68.png', 'imagepy69.png', 'imagepy70.png', 'imagepy71.png', 'imagepy72.png', 'imagepy73.png', 'imagepy74.png', 'imagepy75.png', 'imagepy76.png', 'imagepy77.png', 'imagepy78.png', 'imagepy79.png', 'imagepy80.png', 'imagepy81.png', 'imagepy82.png', 'imagepy83.png', 'imagepy84.png', 'imagepy85.png', 'imagepy86.png', 'imagepy87.png', 'imagepy88.png', 'imagepy89.png', 'imagepy90.png', 'imagepy91.png', 'imagepy92.png', 'imagepy93.png', 'imagepy94.png', 'imagepy95.png', 'imagepy96.png', 'imagepy97.png', 'imagepy98.png', 'imagepy99.png', 'imagepy100.png', 'imagepy101.png', 'imagepy102.png', 'imagepy103.png', 'imagepy104.png', 'imagepy105.png', 'imagepy106.png', 'imagepy107.png', 'imagepy108.png', 'imagepy109.png', 'imagepy110.png', 'imagepy111.png', 'imagepy112.png', 'imagepy113.png', 'imagepy114.png', 'imagepy115.png', 'imagepy116.png', 'imagepy117.png', 'imagepy118.png', 'imagepy119.png', 'imagepy120.png', 'imagepy121.png', 'imagepy122.png', 'imagepy123.png', 'imagepy124.png', 'imagepy125.png', 'imagepy126.png', 'imagepy127.png', 'imagepy128.png', 'imagepy129.png', 'imagepy130.png', 'imagepy131.png', 'imagepy132.png', 'imagepy133.png', 'imagepy134.png', 'imagepy135.png', 'imagepy136.png', 'imagepy137.png', 'imagepy138.png', 'imagepy139.png', 'imagepy140.png', 'imagepy141.png', 'imagepy142.png', 'imagepy143.png', 'imagepy144.png', 'imagepy145.png', 'imagepy146.png', 'imagepy147.png', 'imagepy148.png', 'imagepy149.png', 'imagepy150.png', 'imagepy151.png', 'imagepy152.png', 'imagepy153.png', 'imagepy154.png', 'imagepy155.png', 'imagepy156.png', 'imagepy157.png', 'imagepy158.png', 'imagepy159.png', 'imagepy160.png', 'imagepy161.png', 'imagepy162.png', 'imagepy163.png', 'imagepy164.png', 'imagepy165.png', 'imagepy166.png', 'imagepy167.png', 'imagepy168.png', 'imagepy169.png', 'imagepy170.png', 'imagepy171.png', 'imagepy172.png', 'imagepy173.png', 'imagepy174.png', 'imagepy175.png', 'imagepy176.png', 'imagepy177.png', 'imagepy178.png', 'imagepy179.png', 'imagepy180.png', 'imagepy181.png', 'imagepy182.png', 'imagepy183.png', 'imagepy184.png', 'imagepy185.png', 'imagepy186.png', 'imagepy187.png', 'imagepy188.png', 'imagepy189.png', 'imagepy190.png', 'imagepy191.png', 'imagepy192.png', 'imagepy193.png', 'imagepy194.png', 'imagepy195.png', 'imagepy196.png', 'imagepy197.png', 'imagepy198.png', 'imagepy199.png', 'imagepy200.png']
is this what you were looking for?
How do I copy only a portion of a list to another list. For example, if the length of the list is 105 but only 30 of the randomly selected elements need to be copied to a new list.
This is the code that I have written
for x in range (104):
if len(trainingSet1)>30:
break
trainingSet1[x]= (trainingSet[random.randint(1,103)])
But it keeps giving this error:
Traceback (most recent call last):
File "Q1_2.py", line 82, in <module>
main()
File "Q1_2.py", line 72, in main
trainingSet1[x]= (trainingSet[random.randint(1,103)])
IndexError: list index out of range
The bug is probably here:
trainingSet1[x] = ...
Unless you already populated trainingSet1, you’re trying to assign to
an element that doesn’t exist yet. Use trainingSet1.append(...)
instead.
Initialize the trainingSet1 as trainingSet1 =[] and then try to append values to that instead of using trainingSet1[x] = value . If you really want to assign as you have done in the code you can first initialize the array as trainingSet1 = [0] * 30. This will assign 30 0's to the list and those will be replaced by your randomly selected values later.
How do you solve this problem using python?
You load luggage into container. Once container reaches its limit (100 lbs). You move onto the next container. This is how you load the luggage.
When it is time to unload the data, you will first unload the last loaded container (which is less than 100lbs. And from it , you will remove the first loaded luggage, then second, etc.) Then you will move to the next container.
If the question is not clear.
Here is what the loading array looks like
Say we have 3 containers and here was the loading arrangement
[30,45,15,25,50,20,30,60]
The last item in this array is the first loaded. After the second luggage, the first container could not accommodate a 20lb luggage, so that went into the next container.
20,50,25 went into the second, and 15,45,30 went into the last one.
When it is time to unload, we start with the first luggage of the last container, and move in that order.
Therefore the unloading array is
[15,45,30,20,50,25,60,30]
Is there a function that can convert the input into the output?
Keeping in mind, that each container must be kept under 100 lbs??
Any thoughts or ideas?
Sorry if the question rambles, this is my first question on stack
THANKS!
Here is what I have tried thus far:
A=[15,20,25,45,20,30,65]
output=[20,15,20,45,25,65,30]
def input_output(A):
for i in range(1,len(A)):
B=[]*len(A)
if A[-i]<100:
B[0]=A[-i]
B[i]=A[-i-1]+B[i-1]
if B[i]>100:
for j in range(1,i):
C=[]*len(A)
C[-i]=A[i-len(A)]# Need a subarray to get reversed.
#I don't feel this is going the right direction
# C is my intended output array
You need to think about your algorithm.
You are creating bins based on 100, so construct them and then deconstruct to get your expected output. Using [-1] as the index means always add to the last bin in bins:
A = [15,20,25,45,20,30,65]
bins = [[]]
for i in reversed(A):
if sum(bins[-1]) + i > 100:
bins.append([])
bins[-1].append(i)
>>> bins
[[65, 30], [20, 45, 25], [20, 15]]
So you just need to reverse and flatten the bins and this can be done a number of ways:
>>> [a for b in reversed(bins) for a in b]
[20, 15, 20, 45, 25, 65, 30]
# Or
>>> sum(reversed(bins), [])
[20, 15, 20, 45, 25, 65, 30]
# Or
>>> import itertools as it
>>> list(it.chain.from_iterable(reversed(bins)))
[20, 15, 20, 45, 25, 65, 30]
I got some help earlier today about how to obtain positional information from a dictionary using enumerate(). I will provide the code shortly. However, now that I've found this cool tool, I want to implement it in a different manner to obtain some more information from my dictionary.
I have a dictionary:
length = {'A': [(0,21), (30,41), (70,80), (95,200)] 'B': [(0,42), (70,80)]..etc}
and a file:
A 73
B 15
etc
What I want to do now is to find the difference from the max of the first element in my list from the min from the second element. For example, the difference of 21 and 30. Then I want to add all these differences up until I hit the pair (range) of numbers that the number from my file matches to (if that makes sense).
Here is the code that I've been working on:
import csv
with open('Exome_agg_cons_snps_pct_RefSeq_HGMD_reinitialized.txt') as f:
reader = csv.DictReader(f,delimiter="\t")
for row in reader:
snppos = row['snp_rein']
name = row['isoform']
snpos = int(snppos)
if name in exons:
y = exons[name]
for sd, i in enumerate(exons[name]):
while not snpos<=max(i):
intron = min(i+1) - max(i) #this doesn't work unfortunately. It says I can't add 1 to i
totalintron = 0 + intron
if snpos<=max(i):
exonmin = min(i)
exonnumber = sd+1
print exonnumber,name,totalintron
break
I think it's the sd (indexer) that is confusing me. I don't know how to use it in the this context. The commented out portions are other avenues I've tried but failed to be successful. Any help? I know this is a confusing question and my code might be a little mixed up, but that's because I can't even get an output to correct my other mistakes yet.
I want my output to look like this based on the file provided:
exon name introntotal
3 A 38
1 B 0
To try to provide some help for this question: a critical part of the problem is that I don't think enumerate does what you think it does. Enumerate just numbers the things you are iterating over. So when you go through your for loop, sd will first be 0, then it will be 1... And that's all. In your case, you want to look at adjacent list entries (it seems?), so the more idiomatic ways of looping in python aren't nearly as clean. So you could do something like:
...
y = exons[name]
for index in range(len(y) - 1): # the - 1 is to prevent going out of bounds
first_max = max(y[index])
second_min = min(y[index+1])
... # do more stuff, I didn't completely follow what you're trying to do
I will add for the hardcore pythonistas, you can of course do some clever stuff to write this more idiomatically and avoid the C style loop that I wrote, but I think that getting into zip and so on might be a bit confusing for somebody new to python.
The issue is that you're using the output of enumerate() incorrectly.
enumerate() returns the index (position) first then the item
Ex:
x = [10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
for i, item in enumerate(x):
print(i, item)
# prints
#(0, 10)
#(1, 11)
#(2, 12)
#(3, 13)
#(4, 14)
#(5, 15)
#(6, 16)
#(7, 17)
#(8, 18)
#(9, 19)
So in your case, you should switch i and sd:
for i, sd in enumerate(exons[name]):
# do something
Like other commenters suggested, reading the python documentation is usually a good place to start resolving issues, especially if you're not sure how a function does what it does :)
I have a dictionary called tmptwo that looks like this:
{'test-test': [['2014-01-05 01:06:11', 37, 34, 0], ['2014-01-05 01:09:20', 44, 32, 11]], 'another-test': [['2014-01-05 01:06:11', 88, 76, 6], ['2014-01-05 01:09:20', 62, 9, 3]]}
I am trying to pipe this data into GNUplot over stdin. However I am struggling with writing the loop to do this.
I must plot in the following order:
the time-stamp of the first list followed by one of the three integers of key 'test-test'
the time-stamp of the first list followed by one of the three integers of key 'another-test'
As in 1. but with the next list
As in 2. but with the next list
Of course here there are only two keys so it was easy to explain in 4 steps the algorithm for this particular example. But in my actual script, the dictionary will have multiple keys and multiple lists (though all keys will have the same number of lists within their respective value pair) within the value (which itself is a list) of the key. Do you follow?
I know there's probably an easier way of feeding the data in, but this is what I'm working with...
So far my function for feeding this data into GNUplot looks like this:
def startPlotting(selection, gnuplot, tmptwo):
try:
if selection == 'totals':
sel = 1
if selection == 'usable':
sel = 2
if selection == 'inserted':
sel = 3
for items in zip(*tmptwo.values()):
for item in items:
gnuplot.stdin.write("%s,%i\n" % (item[0],item[sel]))
gnuplot.stdin.write("e\n")
gnuplot.stdin.write("reset\n")
gnuplot.stdin.flush()
except (IOError, TypeError, NameError) as e:
raise
And so far, I'm having no joy. Can you help?
EDIT:
Have updated the loop in the code based on the suggestion below, however GNUplot is still error'ing out. I'm clearly unable to find the solution... Any suggestions?