简体   繁体   English

如何在列表中附加嵌套元组项?

[英]How can I append nested tuple items in a list?

I have a nested tuple t1 = ((1, 'Kamil'), (2, 'Hassaan')) and I want to copy the elements of the tuple into a list like this: [[1, 2], [Kamil, Hassaan]] . 我有一个嵌套的元组t1 = ((1, 'Kamil'), (2, 'Hassaan')) ,我想将元组的元素复制到这样的列表中: [[1, 2], [Kamil, Hassaan]] Take 1 and 2 and combine them. 1 and 2并合并。 Take Kamil and Hassaan and combine them. Kamil and Hassaan合并。 Combine them together in a small list( temp ) and append temp in the List . 将它们组合到一个小的列表( temp )中,并将temp附加到List

This is my code: 这是我的代码:

t1 = ((1, 'Kamil'), 
      (2, 'Hassaan'))

t2 = ((1, 'python', 'print'),
      (2, 'c++', 'cout'))
iSize = len(t1[0])
#print(len(t1))

index = 0
List = []
temp = []

r = 0
c = 0

while r < len(t1[0]):
    while c < len(t1):

        temp.append(t1[c][r])
        c += 1

    List.append(temp)
    print(List)

    c = 0
    temp.clear()
    print(temp)
    r += 1

print(List)

You will see some unnecessary print commands. 您将看到一些不必要的打印命令。 I used them to check my code. 我用它们来检查我的代码。 I can't understand why after one iteration the items of the List get overwritten and then the whole List is empty at the end. 我不明白为什么经过一轮迭代后, List的项目会被覆盖,然后整个List为空。

When you call temp.clear() you are clearing both the temporary variable AND what you just appended to the list. 当您调用temp.clear()您将同时清除临时变量和刚刚添加到列表中的内容。 One way around this is to use copy from the copy module to make a new variable to append to the list, then clearing the temp variable has to effect on your final list. 解决此问题的方法是使用copycopy模块,使追加新的变量列表中,然后清除临时变量,以最终名单上的效果。

Code: 码:

from copy import copy

t1 = ((1, 'Kamil'), 
      (2, 'Hassaan'))

t2 = ((1, 'python', 'print'),
      (2, 'c++', 'cout'))
iSize = len(t1[0])
#print(len(t1))

index = 0
List = []
temp = []

r = 0
c = 0

while r < len(t1[0]):
    while c < len(t1):

        temp.append(t1[c][r])
        c += 1

    List.append(copy(temp))
    print(List)

    c = 0
    temp.clear()
    print(temp)
    r += 1

print(List)

Overall the logic is relatively simple but can be confusing, and this is partially due to naming conventions. 总体而言,逻辑比较简单,但可能会造成混淆,部分原因是命名约定。 It is best to avoid naming variables with class names (in this case List ), you can check Pep-8 for coding standards. 最好避免使用类名来命名变量(在这种情况下为List ),可以检查Pep-8的编码标准。

Further to this, the while loop seems to over-complicate a little the output. 除此之外,while循环似乎使输出有些复杂。 You can simplify with nested for-loops: 您可以使用嵌套的for循环进行简化:

master_list = [[] for element in range(len(t1[0]))]
for inner_tuple in t1:
    for i in range(len(inner_tuple)):
        master_list[i].append(inner_tuple[i])

Hope this helps! 希望这可以帮助!

I'm not sure what the entire process is that you want, but if the result is to have two lists within a list, separated by the type of data (alphabets and numbers) then you can try this: 我不确定您想要的整个过程是什么,但是如果结果是在列表中有两个列表,并用数据类型(字母和数字)隔开,则可以尝试以下操作:

t1 = ((1, 'Kamil'), (2, 'Hassaan'))

list_one = []
list_two = []
result = []
for i in t1:
    list_one.append(i[0])
    list_two.append(i[1])

result.append(list_one)
result.append(list_two)
print(result)

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

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