繁体   English   中英

使用 Python 从数组中查找重复项

[英]Finding duplicates from array using Python

我对 Python 很陌生,想知道是否有一种好方法可以创建新的不重复用户列表。

我的问题是我有这样的东西

[
{
    "userId": "987654321",
    "method": "CARD",
    "lastDigits": "1234",
    "type": "mc",
    "first_name": "Leroy",
    "last_name": "Jenkins",
    "exp": "01/23"
  },
  {
    "userId": "987654321",
    "method": "PAYPAL",
    "first_name": "Leroy",
    "last_name": "Jenkins"
  },
  {
    "userId": "123456789",
    "method": "CARD",
    "lastDigits": "4567",
    "type": "visa",
    "first_name": "Joe",
    "last_name": "Bloggs",
    "exp": "01/25"
  },
  {
    "userId": "46513498000",
    "method": "PAYPAL",
    "first_name": "Betty",
    "last_name": "White"
  }
]

基本上我需要在userId匹配时进行匹配,并在"method": "CARD"而不是 PAYPAL 时保留PAYPAL然后再次重建基本相同的列表,但当用户同时拥有 CARD 和 Z199F4DCF55B9DAFD6AD41CBCFB23 时,减去重复的 userId

::EDIT::用户只能拥有 PAYPAL。 如果它只有 PAYPAL,只需返回

需要示例 output

[
{
    "userId": "987654321",
    "method": "CARD",
    "lastDigits": "1234",
    "type": "mc",
    "first_name": "Leroy",
    "last_name": "Jenkins",
    "exp": "01/23"
  },
  {
    "userId": "123456789",
    "method": "CARD",
    "lastDigits": "4567",
    "type": "visa",
    "first_name": "Joe",
    "last_name": "Bloggs",
    "exp": "01/25"
  },
  {
    "userId": "46513498000",
    "method": "PAYPAL",
    "first_name": "Betty",
    "last_name": "White"
  }
]

如果我没有误解您的问题,那么简单的过滤将为您解决问题,

user_ids = []
final_users = []
for user in users:
    user_ids.append(int(user['userId']))
    if user['userId'] not in user_ids and user['method'] == 'CARD':
        final_users.append(user)
print(final_users)

工作代码: https://rextester.com/COBGVU63922

users = {}

for d in user_list:
    uid = d["userId"]
    # If user id not in users, we add it
    if uid not in users:
        users[uid] = d

    # Otherwise we check if the already recorded method was "PAYPAL", 
    # if so we overwrite it.
    elif users[uid]["method"] == "PAYPAL":
       users[uid] = d

# To convert dict we just created back to list:
user_list = list(users.values())
    

使用defaultdict

from collections import defaultdict

newdata = defaultdict(dict)

for item in data:
    userid = newdata[item['userId']]
    if userid == {} and item['method'] == 'CARD':
        userid.update(item)

Output:

# newdata = list(newdata.values())
>>> newdata
[{'userId': '987654321',
  'method': 'CARD',
  'lastDigits': '1234',
  'type': 'mc',
  'first_name': 'Leroy',
  'last_name': 'Jenkins',
  'exp': '01/23'},
 {'userId': '123456789',
  'method': 'CARD',
  'lastDigits': '4567',
  'type': 'visa',
  'first_name': 'Joe',
  'last_name': 'Bloggs',
  'exp': '01/25'}]

这将非常适合您。 试试看

mylist=[
{
    "userId": "987654321",
    "method": "CARD",
    "lastDigits": "1234",
    "type": "mc",
    "first_name": "Leroy",
    "last_name": "Jenkins",
    "exp": "01/23"
  },
  {
    "userId": "987654321",
    "method": "PAYPAL",
    "first_name": "Leroy",
    "last_name": "Jenkins"
  },
  {
    "userId": "123456789",
    "method": "CARD",
    "lastDigits": "4567",
    "type": "visa",
    "first_name": "Joe",
    "last_name": "Bloggs",
    "exp": "01/25"
  },
  {
    "userId": "46513498000",
    "method": "PAYPAL",
    "first_name": "Betty",
    "last_name": "White"
  }
]
temp_list=[]
temp_id=[]
for x in mylist:
    if int(x['userId']) not in temp_id:
        temp_list.append(x)
        temp_id.append(int(x["userId"]))

print(temp_list)

暂无
暂无

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

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