简体   繁体   English

如何在Python中创建包含各种类型元素的列表?

[英]How can I make a list that contains various types of elements in Python?

I have the following parameters in a Python file that is used to send commands pertaining to boundary conditions to Abaqus: 我在Python文件中有以下参数,用于向Abaqus发送与边界条件有关的命令:

u1=0.0,
u2=0.0,
u3=0.0,
ur1=UNSET,
ur2=0.0,
ur3=UNSET

I would like to place these values inside a list and print that list to a .txt file. 我想将这些值放在列表中,并将该列表打印到.txt文件。 I figured I should convert all contents to strings: 我想我应该将所有内容转换为字符串:

List = [str(u1), str(u2), str(u3), str(ur1), str(ur2), str(ur3)]

This works only as long as the list does not contain "UNSET", which is a command used by Abaqus and is neither an int or str. 只有当列表不包含“UNSET”时,这才有效,这是Abaqus使用的命令,既不是int也不是str。 Any ideas how to deal with that? 任何想法如何处理? Many thanks! 非常感谢!

UNSET is an Abaqus/cae defined symbolic constant. UNSET是Abaqus / cae定义的符号常量。 It has a member name that returns the string representation, so you might do something like this: 它有一个返回字符串表示形式的成员name ,因此您可以执行以下操作:

 def tostring(v):
  try:
   return(v.name)
  except:
   return(str(v))

then do for example 然后做例如

  bc= [0.,1,UNSET]
  print "u1=%s u2=%s u3=%s\n"%tuple([tostring(b) for b in bc])

u1=0. U1 = 0。 u2=1 u3=UNSET u2 = 1 u3 = UNSET

EDIT simpler than that. 编辑比这更简单。 After doing things the hard way I realize the symbolic constant is handled properly by the string conversion so you can just do this: 经过艰苦的处理,我意识到符号常量是通过字符串转换正确处理的,所以你可以这样做:

  print "u1=%s u2=%s u3=%s\n"%tuple(['%s'%b for b in bc])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM