简体   繁体   English

创建一个带有变量和 Boolean Python 的元组

[英]Creating a Tuple with a variable and Boolean Python

I'm supposed to add a variable and a boolean to a new Tuple - the actual assignment is below, with my code.我应该将一个变量和一个 boolean 添加到一个新的元组中——实际的赋值在下面,以及我的代码。 I know tuples are immutable - this is the first time I've tried to make one.我知道元组是不可变的——这是我第一次尝试创建元组。 Additionally, I can't find anything about inserting the variable and a boolean. Thanks in advance!此外,我找不到任何关于插入变量和 boolean 的信息。提前致谢!

My code is just created a new list.我的代码刚刚创建了一个新列表。 This is the desired result:这是期望的结果:

 [('h', False), ('1', True), ('C', False), ('i', False), ('9', True), ('True', False), ('3.1', False), ('8', True), ('F', False), ('4', True), ('j', False)]

Assignment :作业

The string module provides sequences of various types of Python characters. string模块提供了Python个字符的各种类型的序列。 It has an attribute called digits that produces the string '0123456789'.它有一个名为 digits 的属性,它生成字符串“0123456789”。 Import the module and assign this string to the variable nums.导入模块并将此字符串分配给变量 nums。 Below, we have provided a list of characters called chars.下面,我们提供了一个名为 chars 的字符列表。 Using nums and chars, produce a list called is_num that consists of tuples.使用 nums 和 chars,产生一个名为 is_num 的列表,它由元组组成。 The first element of each tuple should be the character from chars, and the second element should be a Boolean that reflects whether or not it is a Python digit.每个元组的第一个元素应该是来自 chars 的字符,第二个元素应该是一个 Boolean 来反映它是否是一个 Python 数字。

import string
nums = string.digits
chars = ['h', '1', 'C', 'i', '9', 'True', '3.1', '8', 'F', '4', 'j']
is_num = []

for item in chars:
    if item in string.digits:
        is_num.insert(item, bool)

elif item not in string.digits:
    is_num.insert(item, bool)

You can use a list comprehension for this, which is like a more concise for loop that creates a new list您可以为此使用列表推导,这就像创建一个新列表的更简洁for循环

>>> from string import digits
>>> chars = ['h', '1', 'C', 'i', '9', 'True', '3.1', '8', 'F', '4', 'j']
>>> is_num = [(i, i in digits) for i in chars]
>>> is_num
[('h', False), ('1', True), ('C', False), ('i', False), ('9', True), ('True', False), ('3.1', False), ('8', True), ('F', False), ('4', True), ('j', False)]

This would be equivalent to the follow loop这相当于跟随循环

is_num = []
for i in chars:
    is_num.append((i, i in digits))

>>> is_num
[('h', False), ('1', True), ('C', False), ('i', False), ('9', True), ('True', False), ('3.1', False), ('8', True), ('F', False), ('4', True), ('j', False)]

Note that the containment check is being done using in against string.digits请注意,正在使用instring.digits进行包含检查

>>> digits
'0123456789'
>>> '7' in digits
True
>>> 'b' in digits
False

the easiest way is that you should have casted the nums in to a list.最简单的方法是您应该将 nums 放入列表中。 i mean num =list(num) before parsing it in我的意思是 num =list(num) 在解析之前

import string
chars = ['h', '1', 'C', 'i', '9', 'True', '3.1', '8', 'F', '4', 'j']
    nums = string.digits
    nums = list(nums)
    is_num = []

for char in chars:
    if char in nums:
        is_num.append((char, True))
    else:
        is_num.append((char, False))
print(is_num)

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

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