如何在字典中处理这个带有表情符号的结构奇怪的字符串列表?

2024-06-12 21:32:58 发布

您现在位置:Python中文网/ 问答频道 /正文

假设我们有一个定义了结构的字符串列表。 解析这样一个列表以获取词典的最简单策略是什么

mylist = [
    '🇺🇸Zynex 0,6',
    '🇺🇸PayPal 11',
    '🇺🇸PetIQ 0,5',
    '🇺🇸First Solar 0,7',
    '🇺🇸Upwork 1%',
    '🇺🇸NV5 Global 0,8',
    '🇺🇸TPI Composites 1',
    '🇺🇸Fiserv 0,5',
]

我希望得到结果:

{
    'Zynex': 0.6,
    'PayPal': 11.0,
    'PetIQ': 0.5,
    'First Solar': 0.7,
    'Upwork': 1.0,
    'NV5 Global': 0.8,
    'TPI Composites': 1.0,
    'Fiserv': 0.5,
}

Tags: 字符串列表定义结构globalpaypalfirstupwork
3条回答

我希望此代码可以帮助您:

mylist = ['🇺🇸Zynex 0,6',
 '🇺🇸PayPal 11',
 '🇺🇸PetIQ 0,5',
 '🇺🇸First Solar 0,7',
 '🇺🇸Upwork 1%',
 '🇺🇸NV5 Global 0,8',
 '🇺🇸TPI Composites 1',
 '🇺🇸Fiserv 0,5']

edited = []
dicto = {}
for val in mylist:
     new_val = val[2:]
     edited.append(new_val)
for i, val in enumerate(edited):
    tmp = val.rsplit(' ', 1)
    dicto[tmp[0]] = tmp[1]
print(dicto)

其实很简单:

import re

mylist = [
    '🇺🇸Zynex 0,6',
    '🇺🇸PayPal 11',
    '🇺🇸PetIQ 0,5',
    '🇺🇸First Solar 0,7',
    '🇺🇸Upwork 1%',
    '🇺🇸NV5 Global 0,8',
    '🇺🇸TPI Composites 1',
    '🇺🇸Fiserv 0,5',
]

res = {}
for elem in mylist:
    key, val = re.sub(r"[^A-Za-z0-9, ]", "", elem).rsplit(" ", 1)
    res[key] = float(val.replace(",", "."))
 
print(res)

Output:

{'Zynex': 0.6, 'PayPal': 11.0, 'PetIQ': 0.5, 'First Solar': 0.7, 'Upwork': 1.0, 'NV5 Global': 0.8, 'TPI Composites': 1.0, 'Fiserv': 0.5}

编辑: 根据您的评论,您还希望获得标志emojis的文本表示。粗溶液是这样的:

def flag_to_str(emoji):
    return "".join(chr(c - 101) for c in emoji.encode()[3::4])


print(flag_to_str("🇺🇸"))  # US
print(flag_to_str("🇫🇮"))  # FI

# How it works:
print("🇺🇸".encode())  # b'\xf0\x9f\x87\xba\xf0\x9f\x87\xb8'
print("🇺🇸".encode()[3::4])  # b'\xba\xb8'
print("🇺🇸".encode()[3::4][0])  # 186
print(chr("🇺🇸".encode()[3::4][0] - 101))  # U

说明: 大多数标志符号被编码为两个regional indicator symbols的序列。例如。🇺🇸 是🇺+ 🇸, 以十六进制表示,表示为f0 9f 87 ba f0 9f 87 b8https://onlineutf8tools.com/convert-utf8-to-hexadecimal?input=🇺🇸&prefix=false&padding=false&spacing=true)。从这里我们可以看到,每个区域符号都以f0 9f 87开头,第四个字节是数量101₁₀ 添加到等效的ASCII大写字符:https://www.asciitable.com。因此0xba<=&燃气轮机;186₁₀ - 101₁₀ = U

我假设该结构包括数字部分,没有空格作为字符串的最后一个组成部分,并且您希望从字符串的前一个组成部分中去掉“us”

您需要的基本过程是在原始列表上迭代,每次遍历时执行以下操作:

  1. 将字符串分为键和值部分
  2. 清除不需要的东西的价值
  3. 清理钥匙上不需要的东西
  4. 将key:value对添加到dicitonary

类似这样的,但我没有研究有百分比的值:

my_list = ['🇺🇸Zynex 0,6',
 '🇺🇸PayPal 11',
 '🇺🇸PetIQ 0,5',
 '🇺🇸First Solar 0,7',
 #'🇺🇸Upwork 1%',
 '🇺🇸NV5 Global 0,8',
 '🇺🇸TPI Composites 1',
 '🇺🇸Fiserv 0,5']

##strip the 'us'
my_list = [x[2:] for x in my_list]
print(my_list[0].lstrip('us'))
##create a dictionary
my_dict = {}

## Now iterate over my_list and add key,value pairs to my_dict.
for e in my_list:
   ## make a list of the string, split on whitespace
   e = e.split()
   ## get the final element as value
   value = e[-1]
   ## replace commas with periods in value
   ## and convert to a float.
   value = float(value.replace(',','.'))
   ##join the rest of e into the key part.
   key = ' '.join(e[:-1])
   my_dict[key] = value

相关问题 更多 >