奇怪的标题,但很容易问一个例子:
mytuple = (1, 2)
mynumber = 3
print (mynumber,) + (mytuple,)
>> (3, (1, 2))
我不想在元组中使用元组。我希望得到(3,1,2)。有什么想法吗?
答案 0 :(得分:1)
您必须检查第二个对象是否为元组:
a = (1, 2)
b = 3
joined = (a if isinstance(a, tuple) else (a,)) + (b if isinstance(b, tuple) else (b,))
答案 1 :(得分:0)
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView"
android:layout_gravity="center"
android:layout_weight="1"
android:textAlignment="center" />
答案 2 :(得分:0)
你可以使用lambda从任何东西中取出元组:
always_tuple = lambda x: x if isinstance(x, tuple) else (x,)
joined = always_tuple(a) + always_tuple(b)