please help me.
I have two-dimensional array example :
self.history = [['23295', u'0.0500', u'0.0700', u'0.0600', u'0.0600'],['23295', u'0.0500', u'0.0700', u'0.0600', u'0.0600']]
i try parsing him and get string but have syntax error, please advice.
for i in range(int(cac)):
returning = returning + "\""+str(date_arr[i])+","+ str(self.history[0 for x in range(len(self.history))][i])+"+"
in output i need have somethings like :
"somedate,'23295','23295'" + "somedate,u'0.0500',u'0.0500'" + "somedate,u'0.0700',u'0.0700'"...
You have to use two for loops:
for x in range(len(self.history)):
for i in range(int(cac)):
returning = returning + "\""+str(date_arr[i])+","+ str(self.history[x][i])+"+"
Note that your code [x for x in range(len(self.history))] generates a list whereas you need an integer to index your list
Related
I have the following set in python (its actually one set item):
product_set = {'Product, Product_Source_System, Product_Number'}
I want to add a static prefix (source.) to all the comma seperated values in the set, so I get the following output:
{'source.Product, source.Product_Source_System, source.Product_Number'}
I tried with a set comprehension, but it doesn't do the trick or I'm doing something wrong. It only prefixes the first value in the set.
{"source." + x for x in set}
I know sets are immutable. I don't need a new set, just output the new values.
Anyone that can help?
Thanks in advance
Edit: Splitting the initial long string into a list of short strings and then (only if required) making a set out of the list:
s1 = set('Product, Product_Source_System, Product_Number'.split(', '))
Constructing a new set:
s1 = {'Product', 'Product_Source_System', 'Product_Number'}
s2 = {"source." + x for x in s1}
Only printing the new strings:
for x in s1:
print("source." + x)
Note: The shown desired result is a new set with updated comma-seperated values. Further down you mentioned: "I don't need a new set, just output the new values". Which one is it? Below an option to mimic your desired result:
import re
set = {'Product, Product_Source_System, Product_Number'}
set = {re.sub(r'^|(,\s*)', r'\1source.', list(set)[0])}
# set = {'source.'+ list(set)[0].replace(', ', ', source.')}
print(set)
Prints:
{'source.Product, source.Product_Source_System, source.Product_Number'}
I started to learning today Python. I am trying to make a Fate dice bot for Discord. I want to replace an integer with a string and I wrote:
zarList = [1,-1,0]
zarsonuc = random.choices(zarList, k=4)
zarsonucsayi = sum(zarsonuc)
zartanim = {-4:'Felaket', -3:'Rezalet', -2:'Kötü', -1:'Dandik', 0:'Düz', 1:'Eh', 2:'İyi', 3:'Baya İyi', 4:'Harika'}
tanimsonuc = [zartanim.get(n,n) for n in zarsonucsayi]
await ctx.send(f"{tanimsonuc} bir zar attın.{sonuc},{zarsonucsayi}")`
But I take this TypeError :(
discord.ext.commands.errors.CommandInvokeError: Command raised an exception: TypeError: 'int' object is not iterable.
Can anyone help me?
If you want to replace an integer to a string you can just cast it to a string. For example, if you want to cast the first number of your array to a string you may say:
str(zarList[0])
Hope it helps,(just answered by what you asked on the title)
Upon looking at your code, the error doesn't appear because the list elements are not a string but because you are trying to iterate over an int element and not an iterable object.
Here:
tanimsonuc = [tanim.get(n,n) for n in zarsonucsayi]
Since
zarsonucsayi = sum(zarsonuc)
returns an int value, and you can't iterate it.
You need to iterate using the range
tanimsonuc = [zartanim[n] for n in range(zar_sonuc_sayi)]
Output:
['Düz', 'Eh', 'İyi', 'Baya İyi']
Explanation
[zartanim.get(n,n) for n in zarsonucsayi]
You need to iterate through zarsonucsayi to do that use range(zarsonucsayi)
You wanted the value of the corresponding dice value, use zartanim[n]
zarList = [str(x) for x in zarList]
basicly you iterate trough the list and convert it to a string using str
I have a problem with my code, I just want to write the result in csv and i got IndexError
seleksi = []
p = FeatureSelection(fiturs, docs)
seleksi[0] = p.select()
with open('test.csv','wb') as selection:
selections = csv.writer(selection)
for x in seleksi:
selections.writerow(selections)
In p.select is:
['A',1]
['B',2]
['C',3]
etc
and i got error in:
seleksi[0] = p.select()
IndexError: list assignment index out of range
Process finished with exit code 1
what should i do?
[], calls __get(index) in background. when you say seleksi[0], you are trying to get value at index 0 of seleksi, which is an empty list.
You should just do:
seleksi = p.select()
When you initlialize a list using
seleksi = []
It is an empty list. The lenght of list is 0.
Hence
seleksi[0]
gives an error.
You need to append to the list for it to get values, something like
seleksi.append(p.select())
If you still want to assign it based on index, initialize it as array of zeros or some dummy value
seleksi = [0]* n
See this: List of zeros in python
You are accesing before assignment on seleksi[0] = p.select(), this should solve it:
seleksi.append(p.select())
Since you are iterating over saleksi I guess that what you really want is to store p.select(), you may want to do seleksi = p.select() instead then.
EDIT:
i got this selections.writerow(selections) _csv.Error: sequence
expected
you want to write x, so selections.writerow(x) is the way to go.
Your final code would look like this:
p = FeatureSelection(fiturs, docs)
seleksi = p.select()
with open('test.csv','wb') as selection:
selections = csv.writer(selection)
for x in seleksi:
selections.writerow(x)
import imgcompare
...
for filename in os.listdir(myPath):
if filename.endswith(".png"):
listIm1.append(filename)
for filename2 in os.listdir(myPath2):
if filename2.endswith(".png"):
listIm2.append(filename2)
so i fill my two lists with images,now I would like to compare the images of the two lists one by one following the same index, for example:listIm1[0] with listImg2[0]listIm1[1] with listImg2[1]and so on... and that's the code:
for item in listIm1:
ifSame = imgcompare.is_equal(listIm1[item],listIm2[item],tolerance=2)
print ifSame
but get the error:
same = imgcompare.is_equal(listIm1[item], listIm2[item], tolerance=2)
TypeError: list indices must be integers, not str
it seems that imgcompare.is_equal() does not work with lists, is there some pythonic expedient to make it
works?
since
if filename2.endswith(".png"):
listIm2.append(filename2)
for item in listIm1:
# item = "someimagine.png"
ifSame = imgcompare.is_equal(listIm1[item],listIm2[item],tolerance=2)
#listIm1[someimagine.png] is what you are asking => retrun Type Error
I guess you are looking for something like this:
edit:
import os
for filename in os.listdir(myPath):
if filename2.endswith(".png"):
img_path = os.path.join(myPath,filename2)
listIm2.append(img_path)
listIm1 = []
listIm2 = []
for i in range(len(listIm1)):
ifSame = imgcompare.is_equal(listIm1[i],listIm2[i],tolerance=2)
print ifSame
and it's better if len(listIm1) == len(listIm2)
The problem here is that you are trying to get the index of listIm1 by using item. What you want to do is use a range(), like:
for i in range(len(listIm1)):
ifSame = imgcompare.is_equal(listIm1[i],listIm2[i],tolerance=2)
As #Matt pointed out, this will only work if you know the lists are the same length beforehand, otherwise it will throw an index error.
You are using a for each loop, which grabs each element in your provided list listIm1 and stores it in a temp variable item, you then pass item (which is a string) as an index both of your lists. Indices of a list must be an integer, and that is the error you are getting.
for dir1_file in listIm1:
for dir2_file in listIm2:
ifSame = imgcompare.is_equal(dir1_file,dir2_file,tolerance=2)
print ifSame
This code uses two for each loops, it looks at each element in both of the lists and uses them as the arguments for your method.
I told myself to never ask common sense or novice questions if I can look it up on the web and to resort to asking people as the last resort. This will probably be a fundamental question but please do enlighten me!
My code :
obj = pm.ls (selection=True,sn=True,o=True) # get selection = obj
shapes = pm.listRelatives(obj) # get obj shapeNode name
cpmNode = pm.createNode('closestPointOnMesh') # create
closestPointOnMesh Node
pm.setAttr(shapes+".outMesh",cpmNode + ".inMesh") # setattr selection
shapeNode to cpmNode inMesh
Error :
# Can only concatenate list (not "str") to list
I don't understand how pm.setAttr(shapes+".outMesh",cpmNode + ".inMesh") is making into a list. Isn't it a command with strings now?
And by putting shapes into pm.setAttr(shapes+".outMesh",cpmNode + ".inMesh") is giving me errors where .outmesh is a string?
Is pm.setAttr(shapes+".outMesh",cpmNode + ".inMesh") a list then?
How should I make my current selection shapeNode to change pCubeShape1 if given example (below):
pm.setAttr("pCubeShape1.outMesh",cpmNode + ".inMesh")
As you suspected, cmds.listRelatives returns a list, not a string. That's because a transform can contain multiple shapes.
Also you cannot use setAttr to connect the shape's outMesh to the utility's inMesh. Instead you need to use connectAttr.
Try this:
import pymel.core as pm
obj = pm.ls (selection=True,sn=True,o=True)
shapes = pm.listRelatives(obj, shapes=True, ni=True, type="mesh") # Use flags to narrow down the proper shape.
cpmNode = pm.createNode('closestPointOnMesh')
pm.connectAttr(shapes[0]+".outMesh",cpmNode + ".inMesh") # Use the first item in the shapes list.