【问题标题】:Create a set in Python and append the values from another list to it在 Python 中创建一个集合并将另一个列表中的值附加到它
【发布时间】:2023-03-21 10:37:01
【问题描述】:

我有列表 2,其中包含列表 1 和 x 的所有项目:

 import random
 for x in range(10):
 print(random.randint(1,101))

 list1 = ''
 list.append(x)
 list2 = list1

如何创建一个集合并将列表 2 中的所有项目添加到一个集合中?到目前为止的所有代码都是我被告知要做的,我不知道如何创建一个集合并将项目附加到它。

【问题讨论】:

标签: python-3.x


【解决方案1】:

也许是这样的。我最后将列表转换为设置。

  #!/usr/bin/env python
    # -*- coding: utf-8 -*-


    import random
    list1 = []

    for x in range(10):
        number = random.randint(1,101)
        list1.append(number)


    list2 = list1
    new_set = set(list2)
    print (new_set)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-18
    • 2016-03-06
    • 2013-07-31
    • 1970-01-01
    相关资源
    最近更新 更多