在各种条件下加入列表元素

发布于 2025-02-03 03:26:11 字数 672 浏览 3 评论 0原文

对于此列表,

pays_list=["France","francais","€200", "1kg","20€","Espagne","espagnol","€20",
"Allemagne","allemand","deutsch","€100","2kg", "300€",
"Belgique","belge","frite","€30"]

pays_concatenate=[]

for i, elm in enumerate(pays_list):
    if "€" in elm:
        del pays_list[i]
    pays_list=pays_list

for i in pays_list:
    for e in i:
        if any(e in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for e in i):
            print(i)

“ i”将等于带有大写字母的元素...(法国,espagne等...)

之前添加元素

我想在下一个大写字母i

pays_concatenate=["France francais","Espagne espagnol",
    "Allemagne allemand deutsch",
    "Belgique belge frite"]

For this list

pays_list=["France","francais","€200", "1kg","20€","Espagne","espagnol","€20",
"Allemagne","allemand","deutsch","€100","2kg", "300€",
"Belgique","belge","frite","€30"]

pays_concatenate=[]

for i, elm in enumerate(pays_list):
    if "€" in elm:
        del pays_list[i]
    pays_list=pays_list

for i in pays_list:
    for e in i:
        if any(e in "ABCDEFGHIJKLMNOPQRSTUVWXYZ" for e in i):
            print(i)

"i" will be equal to elements with a capital letter...(France, Espagne etc ...)

I want to add the elements before the next capital letter

I except this output

pays_concatenate=["France francais","Espagne espagnol",
    "Allemagne allemand deutsch",
    "Belgique belge frite"]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一个人练习一个人 2025-02-10 03:26:11

使用itertools.groupby将字符串分组为具有和不带有“€”的那些字符串,并使用“” :

import itertools

pays_list=["France","francais","€200", "1kg","20€","Espagne","espagnol","€20",
"Allemagne","allemand","deutsch","€100","2kg", "300€",
"Belgique","belge","frite","€30"]


pays_concatenate = [
    " ".join(g) 
    for _, g in itertools.groupby(pays_list, lambda e: "€" in e)
]
print(pays_concatenate)
# ['France francais', '€200', '1kg', '20€', 'Espagne espagnol', '€20',
#  'Allemagne allemand deutsch', '€100', '2kg', '300€',
#  'Belgique belge frite', '€30']

Use itertools.groupby to group the strings into those with and without a "€", and join each group with " ":

import itertools

pays_list=["France","francais","€200", "1kg","20€","Espagne","espagnol","€20",
"Allemagne","allemand","deutsch","€100","2kg", "300€",
"Belgique","belge","frite","€30"]


pays_concatenate = [
    " ".join(g) 
    for _, g in itertools.groupby(pays_list, lambda e: "€" in e)
]
print(pays_concatenate)
# ['France francais', '€200', '1kg', '20€', 'Espagne espagnol', '€20',
#  'Allemagne allemand deutsch', '€100', '2kg', '300€',
#  'Belgique belge frite', '€30']
戈亓 2025-02-10 03:26:11

感谢Samwise,我完成了您的代码以使我的输出

pays_concatenate = [
" ".join(g) 
for _, g in itertools.groupby(pays_list, lambda e: "€" in e)
]


for i,elm in enumerate(pays_concatenate):
    if not any(s in elm for s in ('€', 'kg')):    
        pays_concatenate=elm
        print(pays_concatenate)

和输出:

France francais
Espagne espagnol
Allemagne allemand deutsch
Belgique belge frite

Thank Samwise, i complete your code to have my output

pays_concatenate = [
" ".join(g) 
for _, g in itertools.groupby(pays_list, lambda e: "€" in e)
]


for i,elm in enumerate(pays_concatenate):
    if not any(s in elm for s in ('€', 'kg')):    
        pays_concatenate=elm
        print(pays_concatenate)

and my output :

France francais
Espagne espagnol
Allemagne allemand deutsch
Belgique belge frite
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文