0

Is it possible to generate a line of float numbers using a for loop where it just appends to the line like a list but is not a list. I'm restricted by a command that only understands data given in a line of float numbers.

Or is it possible to convert a list that contains float numbers to a line of float numbers.

eg:

myList = [0.0,0.452,0.6778,0.12]

I want to convert that so it's a list of numbers in a line 0.0 0.452 0.6778 0.12

DYZ
  • 55,249
  • 10
  • 64
  • 93
dege
  • 65
  • 9
  • 2
    Are you talking about creating a *string* of several numbers? – khelwood Jun 23 '18 at 21:41
  • asa far as i know, there is no "line" data structure in python, but you can try tuple or float array -)) – marmeladze Jun 23 '18 at 21:41
  • `floatsAsStringSpaceSeperated = ' '.join( str(x) for x in [0.2, 0.9, 4.2, 99.9999])` => `0.2 0.9 4.2 99.9999` – Patrick Artner Jun 23 '18 at 21:42
  • 2
    Is this "command" a Python function that takes a str or do you need to output that to another program? – Olivier Melançon Jun 23 '18 at 21:44
  • I don't need it as a string list. The command doesn't understand it. It's a command that I have to run in maya called setAttr where in this instance I'm copying over a whole bunch of values from a vertex to another and it doesn't work if I use a for loop to go through each vertex, so I have to enter in all the values of all the floats at 1 go if that makes sense. – dege Jun 23 '18 at 21:47
  • if I use anything other than a float I get a error reading data element number error – dege Jun 23 '18 at 21:48
  • 1
    Looking at the [Maya Python docs for `setAttr`](http://help.autodesk.com/cloudhelp/2017/ENU/Maya-Tech-Docs/CommandsPython/setAttr.html), I don't think `setAttr` actually wants whatever format you're thinking of. You may be mixing it up with the [MEL version](http://help.autodesk.com/cloudhelp/2018/ENU/Maya-Tech-Docs/Commands/setAttr.html). MEL uses space-separated arguments, but that's not how Python syntax works. – user2357112 Jun 23 '18 at 21:55
  • @user2357112 yes you're right, I was looking at the wrong document. Oh man what a waste of a day. Thanks for the help! – dege Jun 23 '18 at 22:22

2 Answers2

2

You can join the list to get a string:

result = ' '.join(str(x) for x in myList)
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • @OlivierMelançon Looks like the `.` are the decimal points from the original floats, not the joinining character. – Mureinik Jun 23 '18 at 21:45
  • Yes I realized that and deleted my comment. I just misread the output before someone formatted it correctly – Olivier Melançon Jun 23 '18 at 21:45
  • 1
    Small detail though: Join needs a list so better wrap it around a list (comprehension) instead of generator. – Anton vBR Jun 23 '18 at 21:46
  • 2
    @AntonvBR `join` needs an iterable. It will work just fine with such a generator. See e.g. https://repl.it/@AllonMureinik/JoinIterable – Mureinik Jun 23 '18 at 21:49
  • What I have seen around here and reading the docs the str join converts an iterable to a list. By doing a list comprehension you override that step. I'm looking for the reference. Here for instance: https://stackoverflow.com/a/34822788/7386332 – Anton vBR Jun 23 '18 at 21:52
1

Another way to get to the same answer:

' '.join(map(str, myList))
#'0.0 0.452 0.6778 0.12'
DYZ
  • 55,249
  • 10
  • 64
  • 93