## 如何将嵌套的python列表转换为c结构？内容来源于 Stack Overflow，并遵循CC BY-SA 3.0许可协议进行翻译与使用

• 回答 (1)
• 关注 (0)
• 查看 (37)

``````def test(liste):
for inner_l in liste:
for item in inner_l:
print(item)
``````

``````def takeFourth(elem):
return elem[3]

neueListe = neueListe + x.sort(key=takeFourth)
``````

``````struct SubStructOfSubStruct
{
int MyInteger;
};

struct ThirdSubStructType
{
float MyFloatValue;
double MyDoubleValue;
struct SubStructOfSubStructType SubStructOfSubStruct;
};

struct SubStructType
{
float MyFloatValue;
double MyDoubleValue;
};

struct SecondSubStructType
{
int MyInteger;
};

struct Toplevel
{
struct ThirdSubStructType ThirdSubStruct;
struct SubStructType SubStruct;
char MyString[10];
boolean MyBoolValue;
double MyDoubleValue;
float MyFloatValue;
int MyInteger;
struct SecondSubStructType SecondSubStruct;
};
``````

``````[['SubStructOfSubStructType ', [['Integer', 'MyInteger', 33, 0]]],
['ThirdSubStructType ', [['TreeNode', 'SubStructOfSubStructType', 'SubStructOfSubStruct', 2], ['Double', 'MyDoubleValue', 100, 0], ['Float', 'MyFloatValue', 22, 1]]],
['SecondSubStructType', [['Integer', 'MyInteger', 333, 0]]],
['SubStructType', [['Double', 'MyDoubleValue', 1000, 0], ['Float', 'MyFloatValue', 222, 1]]],
['Toplevel', [['TreeNode', 'ThirdSubStructType', 'ThirdSubStruct', 7], ['Float', 'MyFloatValue', 2, 1], ['Boolean', 'MyBoolValue', False, 2], ['Double', 'MyDoubleValue', 10, 0], ['Integer', 'MyInteger', 3, 3], ['TreeNode', 'SecondSubStructType', 'SecondSubStruct', 6], ['String', 'MyString', 'Leer', 4], ['TreeNode', 'SubStructType','SubStruct',5]]]]
``````

### 1 个回答

``````def test(liste):
if isinstance(liste,list):
for inner_l in liste:
test(inner_l)
else:
print('item: {}'.format(liste))
``````

``````def is_struct(liste):
return not isinstance(liste[0],list) and isinstance(liste[1],list) and len(liste) == 2
``````

``````def test(liste):
if isinstance(liste,list):
if is_struct(liste):
print('struct {}'.format(liste[0]))
for item in liste[1]:
print('  var: {}'.format(item))
else:
for inner_l in liste:
test(inner_l)
else:
print('unexpected: {}'.format(liste))
``````