整数列表在Elixir中打印为String

时间:2016-10-30 00:14:55

标签: elixir

我正在尝试Enum.map。当我向列表的所有元素添加100时,我发现了这个奇怪的输出。

Terminal Screenshot

为什么这样输出?事实证明,当我添加100时我得到一个字符串但是当我做其他操作时工作得很好。我摆弄了一些,我仍然得到了这样的意外结果。

Terminal Screenshot 2

1 个答案:

答案 0 :(得分:8)

您看到的值'efgh'不是字符串,而是Charlist

你的陈述结果应该是[101, 102, 103, 104](实际上是),但它并没有这样输出。列表中的四个值以ASCII格式映射到efgh,因此iex只打印codepoints而不是'efgh'列表。如果列表包含无效字符(例如0或433,如您的情况),则将其保留为简单列表。

来自Elixir' Getting Started guide

  

字符列表包含单引号之间字符的代码点(请注意,默认情况下,如果任何字符超出ASCII范围,IEx将仅输出代码点。)

Elixir中的[101, 102, 103, 104]inspect都相同,并证明您可以强制[101, 102, 103, 104] == 'efgh' #=> true [101, 102, 103, 104] |> inspect(charlists: :as_lists) #=> [101, 102, 103, 104] 将其打印为List:

IEx.configure(inspect: [charlists: :as_lists])

您还可以将IEx配置为始终将列表打印为列表:

def CSVtodb(csvfile):
"""
this function adds a mass import of contacts into the database. We do some checks
to make sure it is safe to add them, we show the user a list of any contacts
that were no added.
:param csvfile: the file we take get the contacts from
:return: a template showing the results, how many added, how many errors.
"""
noPhonelist = []
duplicateEntry = []
with open('tmp/{}'.format(csvfile)) as contacts:
    dictContacts = csv.DictReader(contacts)
    for row in dictContacts:
        # Strip all non int from phone numbers
        cellStrip = ''.join(c for c in row['Mobile Phone'] if c.isdigit())
        homeStrip = ''.join(c for c in row['Home Phone'] if c.isdigit())
        otherStrip = ''.join(c for c in row['Other Phone'] if c.isdigit())
        firstname = row['First Name']
        lastname = row['Last Name']
        email = row['E-mail Address']
        print(cellStrip)
        if print(duplicateCheck(cellStrip, homeStrip, otherStrip)) is False:
            if cellStrip == '' and homeStrip == '' and otherStrip == '':
                 noPhone = firstname + ' ' + lastname
                 noPhonelist.append(noPhone)
                 print('no phone')
            else:
                print('added client')

        else:
            print('duplicate entry')

def duplicateCheck(cell, home, other):
    duplist = []
    search = Clients.query.all()
    print(cell, home, other)
    for row in search:
        duplist.append(row.cellNum)
        duplist.append(row.homeNum)
        duplist.append(row.otherNum)
    print(duplist)
    if cell in duplist or home in duplist or other in duplist:
        boolCheck = True
    else:
        boolCheck = False
    print(boolCheck)
    return boolCheck